RSpec:before(:all) 和 before(:each) 之间的深入差异 [英] RSpec: in-depth differences between before(:all) and before(:each)

查看:43
本文介绍了RSpec:before(:all) 和 before(:each) 之间的深入差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,所以我遇到了一个非常奇怪的问题,直接与 before 块相关.我正在通过 Watir 和 RSpec 进行集成测试.对于检查用户是否可以执行登录的简单测试,我通过 factory_girl 在数据库中创建了一个用户"记录.

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.

所以我把下面的代码:

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

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

do stuff 中,我调用浏览器并查看用户如何尝试登录.不幸的是,不知何故他不能这样做——用户名无效".

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".

经过一番调查,我发现如果我将创建用户的代码放在 before(:all) 块中,一切都会神奇地运行.怎么样?在这种情况下,:all:each 之间有什么区别?另外,如果我将用于创建用户的代码实际放在测试正文中,它仍然不起作用(即用户不知何故未添加到数据库或其他东西).

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).

推荐答案

您可能启用了事务性装置,因此您的 Watir 进程不会看到每个 RSpec 示例所包含的事务内的数据库更改.

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.

这篇关于RSpec:before(:all) 和 before(:each) 之间的深入差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆