使用 allocate() 无法通过 Rspec 测试
这是我当前的 users_controller_spec.rb 文件
require 'spec_helper'
describe UsersController do
render_views
.
.
.
describe "success" do
before(:each) do
@attr = { :name => "New User", :email => "[email protected]",
:password => "foobar", :password_confirmation => "foobar" }
end
it "should create a user" do
lambda do
post :create, :user => @attr
end.should change(User, :count).by(1)
end
it "should redirect to the user show page" do
post :create, :user => @attr
response.should redirect_to(user_path(assigns(:user)))
end
end
end
end
当我运行这个文件时,我得到以下信息:
Failures:
1) UsersController POST 'create' success should redirect to the user show page
Failure/Error: response.should redirect_to(user_path(user))
ActionController::RoutingError:
No route matches {:action=>"show", :controller=>"users"}
# ./spec/controllers/users_controller_spec.rb:95:in `block (4 levels) in <top (required)>'
这让我相信 :user 不是一个实际的对象。我如何测试这个以及如何将 :user 更改为 user_path 可以理解的对象。
预先感谢您的任何帮助。
更新:
def create
@title = "Sign up"
@user = User.new(params[:user])
if @user.save
redirect_to @user, :notice => "Signed Up!"
else
@title = "Sign up"
render "new"
end
end
当我运行以下命令时:
it "should redirect to the user show page" do
post :create, :user => @attr
user = assigns(:user)
user.should_not be_blank
puts "user errors are: #{user.errors.full_messages.inspect}" unless user.is_valid?
user.should be_valid
response.should redirect_to(user_path(user))
end
我得到:
1) UsersController POST 'create' success should redirect to the user show page
Failure/Error: user.should_not be_blank
expected blank? to return false, got true
# ./spec/controllers/users_controller_spec.rb:94:in `block (4 levels) in <top (required)>'
Here is my current users_controller_spec.rb file
require 'spec_helper'
describe UsersController do
render_views
.
.
.
describe "success" do
before(:each) do
@attr = { :name => "New User", :email => "[email protected]",
:password => "foobar", :password_confirmation => "foobar" }
end
it "should create a user" do
lambda do
post :create, :user => @attr
end.should change(User, :count).by(1)
end
it "should redirect to the user show page" do
post :create, :user => @attr
response.should redirect_to(user_path(assigns(:user)))
end
end
end
end
When I run this I get the following:
Failures:
1) UsersController POST 'create' success should redirect to the user show page
Failure/Error: response.should redirect_to(user_path(user))
ActionController::RoutingError:
No route matches {:action=>"show", :controller=>"users"}
# ./spec/controllers/users_controller_spec.rb:95:in `block (4 levels) in <top (required)>'
Which leads me to believe that :user isn't an actual object. How can I test this and how can I change :user into an object that user_path understands.
Thanks in advance for any help.
UPDATED:
def create
@title = "Sign up"
@user = User.new(params[:user])
if @user.save
redirect_to @user, :notice => "Signed Up!"
else
@title = "Sign up"
render "new"
end
end
When I run the following:
it "should redirect to the user show page" do
post :create, :user => @attr
user = assigns(:user)
user.should_not be_blank
puts "user errors are: #{user.errors.full_messages.inspect}" unless user.is_valid?
user.should be_valid
response.should redirect_to(user_path(user))
end
I get:
1) UsersController POST 'create' success should redirect to the user show page
Failure/Error: user.should_not be_blank
expected blank? to return false, got true
# ./spec/controllers/users_controller_spec.rb:94:in `block (4 levels) in <top (required)>'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试使用带有方括号而不是圆括号的
赋值
。assigns[:user]
(RSpec 文档< /a>)
编辑
在测试控制器操作时我总是使用的一种模式是确保分配关联视图所需的变量。例如,
Try using
assigns
with brackets instead of parenthesis.assigns[:user]
(RSpec docs)
EDIT
One pattern I always use when testing controller actions is to ensure that a variable required by the associated view(s) is assigned. For example,
你有一个错字
应该是
编辑:
尝试检查用户是否有效:
我知道它适用于之前的测试用例...但仍然值得在这里至少进行一次广泛检查。当你确定时,你可以删除所有这些废话。
You've got a typo
should be
Edit:
Try checking that the user is valid with:
I know it works on the previous test case... but still worth checking extensively at least once here. You can remove all that guff when you're certain.
在您的测试中:
您的
@attr
哈希值与 @user 不同。您正在创建具有 @attr 属性的用户,并断言它应该重定向到 @user show (这与创建的用户不同)。更改为:In your test:
Your
@attr
hash is different from @user. You are creating user with attributes of @attr, and asserting that it should redirect to @user show (which is different from one created). Change to: