本文介绍了用FactoryGirl创建的记录在Controller中不可用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的一项测试开始失败。它以前可以正常工作,并且在浏览器中手动执行相同的操作即可显示该功能正在运行。

One of my tests has started failing. It previously worked and going through the same actions by hand in the browser show the feature to be working.

我创建要测试的记录,如下所示:

I create the records to be tested like this:

will = FactoryGirl.create(:user, profile: "security of the internet", first_name: "will")

为了调查,我添加了

binding.pry 

在我的控制器中调用。该操作将搜索用户。

call in my controller. The action searches Users.

如果我这样做:

User.all

在撬动提示下,没有显示用户,结果为空。

at the pry prompt, no users are shown, an empty result.

如果我用撬撬闯入实际测试,User.all将显示预期的记录。

If i break into the actual test with pry, User.all shows the expected records.

这可能是什么原因?

推荐答案

通常是因为您的测试标记为js:true,并且正在使用具有JS功能的驱动程序,但是您正在使用事务性数据库测试。使用完整浏览器驱动程序时,应用程序和测试将运行。不同的线程,因此它们每个都有自己的数据库连接。这意味着在一个线程事务中创建的对象直到提交后才在另一个线程中可见,但是当使用基于事务的测试时,任何东西都不会提交。要解决此问题,请关闭事务测试并对该测试使用截断或删除-请参见database_cleaner

Usually this is because your test is tagged js: true and is using a JS capable driver but you are using transactional database testing. When using the "full browser" drivers the app and tests run. Different threads so they each have their own database connection. This means objects created in one threads transaction aren't visible in the other until committed, but when using transaction based testing nothing ever gets committed. To fix, turn off transactional testing and use truncation or deletion for that test - see database_cleaner

这篇关于用FactoryGirl创建的记录在Controller中不可用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 12:17