2014-07-24 71 views
3

让Polergeist和RSpec很好地一起玩是我的难题。Rspec不会创建可从poltergeist访问的数据库记录

我已经写了下面的测试:

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: "[email protected]" 
    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: "[email protected]") 
    visit '/runners/axel' 
    debugger 

实际上,上面有哪些做的是注册与密码,“letmein”“新管理”,然后试图查看“亚军的个人资料页阿克塞尔”。

凡调试中断,我可以看到,@ runner_1(阿克塞尔)已创建:

Runner.friendly.find('axel') 

>> #<Runner id: 2, email: "[email protected]",........> 

但是,试图访问时 '/跑步/轴',骚灵报道:

ActiveRecord::RecordNotFound 

这不是路线或任何类似的问题。

在进一步探究这个bug之后,事实上,似乎在测试文件中创建的任何东西都不会在Poltergeist访问的环境中设置。

我似乎无法理解为什么。任何帮助不胜感激。

回答

3

很可能你正在使用rspec中的“交易装置”。这意味着每个测试都在数据库事务中运行,并在测试结束时回滚,以便每个测试都有一个干净的数据库。

其他线程/程序可以不是看看事务正在发生什么。 Poltergeist在单独的线程中运行服务器,这意味着它不能在rspec中看到写入数据库的任何内容(尽管可以直接从rspec代码访问它)。

a description of this phenomenon on the capybara homepage。解决方案是禁用rspec-rails中的事务特性,并在测试后使用类似DatabaseCleaner的方法来重置数据库。

这将工作,但不幸的是截断或删除数据库内容比事务处理方法稍慢 - 这就是为什么tranasactions是默认首先。