Rspec、shoulda 和 spork 不能一起工作
当我运行 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我有同样的问题。通过在 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 requiringrspec/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)尝试将该
行移至
Spork.each_run
块中。显然,shoulda 做了一些魔法将匹配器包含到适当的示例组中。Try moving the
line into the
Spork.each_run
block. Apparently, shoulda does some magic to include the matchers into the appropriate example groups.