Rspec, shoulda and spork does not work together
Asked Answered
M

2

16

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'
...
Mither answered 8/10, 2010 at 20:53 Comment(0)
F
16

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)

Fortdefrance answered 19/10, 2010 at 9:42 Comment(3)
For current shoulda-matcher try require 'shoulda/matchers/integrations/rspec'Dupre
You might find some help on this wiki page: github.com/timcharper/spork/wiki/Spork.trap_method-JujutsuShoffner
For me this worked: require 'shoulda/matchers/integrations/rspec'Hasp
F
2

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.

Fated answered 10/10, 2010 at 21:18 Comment(1)
I moved require 'shoulda' into Sprok.prefork and it worked for me.Cider

© 2022 - 2024 — McMap. All rights reserved.