Rspec、shoulda 和 spork 不能一起工作

发布于 2024-09-26 21:13:00 字数 662 浏览 3 评论 0原文

当我运行 rspec spec/models 结果正常。

但是当我使用 spork 时,每次使用 shoulda 宏(如 it { should validate_presence_of(:title) } )的测试都会失败,并出现如下错误: undefined method 'validate_presence_of ' for ...

我使用:

rails (3.0.0)
shoulda (2.11.3)
spork (0.8.4)
rspec-rails (>= 2.0.0.beta.22)

spec/spec_helper.rb:

require 'rubygems'
require 'spork'

Spork.prefork do
  # This file is copied to spec/ when you run 'rails generate rspec:install'
  ENV["RAILS_ENV"] ||= 'test'
  require File.expand_path("../../config/environment", __FILE__)
  require 'rspec/rails'
  require 'shoulda'
...

when I run rspec spec/models result is OK.

But when I use spork, every test where shoulda macros (like it { should validate_presence_of(:title) } is used FAILS with error like: undefined method 'validate_presence_of' for ...

I use:

rails (3.0.0)
shoulda (2.11.3)
spork (0.8.4)
rspec-rails (>= 2.0.0.beta.22)

spec/spec_helper.rb:

require 'rubygems'
require 'spork'

Spork.prefork do
  # This file is copied to spec/ when you run 'rails generate rspec:install'
  ENV["RAILS_ENV"] ||= 'test'
  require File.expand_path("../../config/environment", __FILE__)
  require 'rspec/rails'
  require 'shoulda'
...

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

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

发布评论

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

评论(2

你丑哭了我 2024-10-03 21:13:00

我有同样的问题。通过在 prefork 块中 require rspec/rails 之后粘贴 require 'shoulda/integrations/rspec2' 来修复此问题。

您可能还想将 spork 升级到最新版本 (gem 'spork', >= 0.9.0.rc2),因为我没有在 0.8.4 上尝试此修复(尽管我我很确定它也会起作用)

I had the same issue. Fixed it by sticking require 'shoulda/integrations/rspec2' after requiring rspec/rails in prefork block.

You might also want to upgrade your spork to the latest version (gem 'spork', >= 0.9.0.rc2), since I didn't try this fix on 0.8.4 (although I am pretty sure it'll work too)

别闹i 2024-10-03 21:13:00

尝试将该

require "shoulda"

行移至 Spork.each_run 块中。显然,shoulda 做了一些魔法将匹配器包含到适当的示例组中。

Try moving the

require "shoulda"

line into the Spork.each_run block. Apparently, shoulda does some magic to include the matchers into the appropriate example groups.

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