Find a newly created record in a controller test using RSpec 3 and Rails 4

丶灬走出姿态 提交于 2019-12-06 08:10:22
Johnny Dương

If a controller has an instance variable named @user, then we can access it in RSpec by assigns(:user).

It is not a good idea to compare records or objects in controller tests. In a controller create test, you should test the correct redirection and the changing of the record.

You can easily compare your objects in a model test, because you can easily track your record.

Still, you can access the created record from a test if your action has a variable that holds the record like

In controller

 def create 
   @user = #assign user
 end

In Test

assigns(:user).name.should eq "Name" # If it has a name attribute
assigns(:user).pending.should be_true # I don't know how you implemented pending

You can take a look at this article

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!