我很难让 Poltergeist 和 RSpec 很好地一起玩。
我编写了以下测试:
it "allows the trainer to view a runner" do
visit '/'
all(:xpath,'//a[@id="get-started"]').first.click
fill_in :name, with: "New Admin"
fill_in :email, with: "admin@test.org"
fill_in :password, with: "letmein"
fill_in :password_confirmation, with: "letmein"
all(:xpath,'//input[@id="get-started-submit"]').first.click
@runner_1 = FactoryGirl.create(:runner, name: "Axel", email: "axel@test.org")
visit '/runners/axel'
debugger
实际上,上面所做的是使用密码“letmein”注册“新管理员”,然后尝试查看“Axel”的运行者个人资料页面。
在调试器中断的地方,我可以看到 @runner_1 (Axel) 已经创建:
Runner.friendly.find('axel')
>> #<Runner id: 2, email: "axel.manzano@hotmail.fr",........>
但是,当尝试访问“/runners/axel”时,Poltergeist 报告:
ActiveRecord::RecordNotFound
这不是路线问题,或者类似的问题。
进一步探索了这个错误,事实上,似乎在测试文件中创建的任何东西实际上都没有在 Poltergeist 访问的环境中设置。
我似乎无法理解为什么。非常感谢任何帮助。
最佳答案
很有可能,您正在 rspec 中使用“事务性装置”。这意味着每个测试都在一个数据库事务中运行,在测试结束时回滚,这样每个测试都有一个干净的数据库。
其他线程/程序可以 不是 看看交易中发生了什么。 Poltergeist 在单独的线程中运行服务器,这意味着它在 rspec 中看不到写入数据库的任何内容(尽管可以直接从 rspec 代码中访问它)。
有a description of this phenomenon on the capybara homepage .解决方案是禁用 rspec-rails 中的事务功能,并使用 DatabaseCleaner 之类的东西在测试后重置数据库。
这会起作用,但不幸的是,截断或删除数据库内容比使用事务方法要慢一些 - 这就是为什么事务首先是默认的。
关于ruby-on-rails - Rspec 没有创建可从 poltergeist 访问的数据库记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24932947/