如何使用Spork、RSpec & 解决cache_clases = true/false 冲突黄瓜?

发布于 2024-12-28 14:16:16 字数 775 浏览 1 评论 0 原文

我在 RSpec 中遇到了很长时间的问题,因为我的模型在 Spork 运行之间没有重新加载。

我最终通过更改 config/environments/test.rb 中的以下行解决了这个问题:

config.cache_classes = true

然而

config.cache_classes = false

...虽然这解决了 RSpec 的问题,但 Cucumber 要求将 cache_classes 设置为 true< /强>。 https://rspec.lighthouseapp.com/projects/16211/tickets/165

是否有一个适用于 RSpec 和 Cucumber 并成功重新加载 RSpec 模型的规范解决方案?

脚注

* 我为使 RSpec 重新加载模型而实现的其他更改包括将以下行添加到 spec_helper.rb

ActiveSupport::Dependencies.clear
FactoryGirl.reload

如果不设置 cache_classes,这些行都无法解决问题=true 行。

I had problems for ages in RSpec with my models not being reloaded between Spork runs.

I eventually fixed this by changing the following line in config/environments/test.rb:

config.cache_classes = true

became

config.cache_classes = false

HOWEVER... while this solved the issues for RSpec, Cucumber requires that cache_classes is set to true.
https://rspec.lighthouseapp.com/projects/16211/tickets/165

Is there a canonical solution to this that works for both RSpec and Cucumber and which reloads RSpec models successfully?

Footnote

* Other changes that I implemented to make RSpec reload models included adding the following lines to spec_helper.rb:

ActiveSupport::Dependencies.clear
FactoryGirl.reload

None of these lines solved the problem without also setting cache_classes=true line.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

怂人 2025-01-04 14:16:16

我遇到了这个问题,对我来说这是由于 Rails 的线程安全模式造成的。确保config.threadsafe!在我的测试环境中没有被调用,为我修复了它。这是因为线程安全模式会阻止代码在每个请求上重新加载,并禁用自动依赖项加载,我猜 spork 在each_run 中依赖于此。

我正在调用 config.threadsafe!在 application.rb 中,所以我从那里删除了该调用,并将其放在development.rb 和 Production.rb 中。现在我可以将 config.cache_classes 设置为 true,并且 rspec 和 cucumber 在 spork 下愉快地工作。请注意,我在持久层和 Machinist 中使用 Mongoid,而不是 FactoryGirl,所以 YMMV。

另请参阅http://rickyrobinson.id.au/2012/07/20/when-spork-puts-a-fork-in-your-cucumber-and-a-spanner-in-your-specs< /a>

I had this problem, and for me it was due to Rails' threadsafe mode. Ensuring config.threadsafe! is not called in my test environment fixed it for me. This is because threadsafe mode prevents code from reloading on each request and disables automatic dependency loading, which I guess spork was relying on in each_run.

I was calling config.threadsafe! in application.rb, so I removed that call from there, and put it in development.rb and production.rb instead. Now I can leave config.cache_classes set to true, and rspec and cucumber work happily under spork. Note that I was using Mongoid in my persistence layer and Machinist instead of FactoryGirl so YMMV.

See also http://rickyrobinson.id.au/2012/07/20/when-spork-puts-a-fork-in-your-cucumber-and-a-spanner-in-your-specs

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文