Rails render_to_string 问题
我正在尝试使用 rspec 测试我的控制器,但总是出现错误。
users_controller.rb:
def update
@user.update_attributes!(params[:user])
redirect_to @user, :status => 202, :text => render_to_string(:partial => "users/show", :type => "json", :locals => {:user => @user})
#notice, that redirect_to was reinitialized and :text is a parameter for response_body
end
_show.tokamakuser {
id user.id
email user.email
username user.username
}
spec fileit "should NOT update user username" do
username = @user.username
put 'update', :id => @user.id, :user => {:username => username+"abc"}, :format => :json
response.status.should be(202)
response.headers["Location"].length.should be > 0
puts response.body
@user.reload
@user.username.should eq(username)
end
end
所以我收到错误:
失败/错误:输入“更新”,:id => @user.id,:用户=> {:用户名=> 用户名+“abc”},:格式=> :json ActionView::模板::错误: 当你没有预料到的时候,你得到了一个 nil 对象! 您可能期望一个 Array 的实例。 计算 nil 时发生错误。[] # C:/Users/makaroni4/free_frog/ffapi/app/views/users/_show.tokamak:1:in
_app_views_users__show_tokamak___509498818 _32151168_368311673' # C:/Users/makaroni4/XXX/XXX/app/controllers/users_controller.rb:22:in 更新' # ./users_controller_spec.rb:34:in `块(4级)在'
那么我调用 render_to_string 方法可能是错误的吗?
I`m trying to test my controller with rspec and always get an error.
users_controller.rb:
def update
@user.update_attributes!(params[:user])
redirect_to @user, :status => 202, :text => render_to_string(:partial => "users/show", :type => "json", :locals => {:user => @user})
#notice, that redirect_to was reinitialized and :text is a parameter for response_body
end
_show.tokamak
user {
id user.id
email user.email
username user.username
}
spec file
it "should NOT update user username" do
username = @user.username
put 'update', :id => @user.id, :user => {:username => username+"abc"}, :format => :json
response.status.should be(202)
response.headers["Location"].length.should be > 0
puts response.body
@user.reload
@user.username.should eq(username)
end
end
So I get an error:
Failure/Error: put 'update', :id =>
@user.id, :user => {:username =>
username+"abc"}, :format => :json
ActionView::Template::Error:
You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.[]
# C:/Users/makaroni4/free_frog/ffapi/app/views/users/_show.tokamak:1:in_app_views_users__show_tokamak___509498818
update'
_32151168_368311673'
# C:/Users/makaroni4/XXX/XXX/app/controllers/users_controller.rb:22:in
# ./users_controller_spec.rb:34:in
`block (4 levels) in '
So may be I call render_to_string method wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试删除发现?
老实说,我会更进一步,确保您模拟并存根 User 对象(或任何 @user 类)的大部分相关行为。请记住,您只是测试控制器操作是否返回您所期望的结果(如果您提供有效输入),而不是模型本身执行正确的操作。
我很难理解模型与控制器规格的差异...
我希望这有帮助...如果没有,我提前道歉...
编辑:
我会更进一步并建议这个测试实际上是一个模型测试。实际的控制器测试将类似于您的规范测试的行为方式:
Try stubbing out find?
To be honest I'd go a few steps further and make sure you mock and stub most of the relevant behavior of the User object (or whatever class @user is). Keep in mind you're only testing that the controller action returns what you expect if you give it valid input--not that the model itself does the right thing.
I had a lot of difficulty wrapping my head around the differences in model vs. controller specs...
I hope this helps...if not, I apologize in advance...
EDIT:
I'll take this a step futher and suggest this test is actually a model test. The actual controller test would be something like as the way your spec test should behave: