我正在尝试使用 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.tokamak
user {
id user.id
email user.email
username user.username
}
规格文件
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
所以我得到一个错误:
失败/错误:输入'update',:id => @user.id, :user => {:username => username+"abc"}, :format => :json ActionView::Template::Error: You have a 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
update' # ./users_controller_spec.rb:34:in `block (4水平)在'
那么我可能调用 render_to_string 方法错了吗?