Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rspec, shoulda and spork does not work together

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'
...
like image 209
boblin Avatar asked Oct 08 '10 20:10

boblin


2 Answers

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)

like image 79
artemave Avatar answered Sep 29 '22 07:09

artemave


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.

like image 41
Jacob Avatar answered Sep 29 '22 05:09

Jacob