RSpec: in-depth differences between before(:all) and before(:each)

别等时光非礼了梦想. 提交于 2019-12-22 04:42:57

问题


Ok, so I've ran into a very strange issue, directly connected with before blocks. I'm doing a integration testing via Watir and RSpec. For a simple test to check if user can perform a login I'm creating a 'user' record in the db by means of factory_girl.

So I put the following code:

before(:each) do
  @user = Factory(:user)
end

if "should perform a login" do
  # do stuff
end

In do stuff I call a browser and see how the user tries to login. Unfortunately, somehow he cannot do that — "Username isn't valid".

After some investigation I discovered that if I put the code for creating user in before(:all) block, everything magically works. How's that? What's the difference between :all and :each in this context? Also, If I put the code for creating user actually in the test body, it still doesn't work (i.e. user somehow isn't added to the DB or something).


回答1:


You probably have transactional fixtures enabled, thus your Watir process doesn't see database changes inside the transaction that each RSpec example is wrapped in.

Try disabling transactional features and use something like database cleaner to get a clean slate before each example.



来源:https://stackoverflow.com/questions/2600821/rspec-in-depth-differences-between-beforeall-and-beforeeach

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