0

I have some test using rails spec (and also capybara and factory girl) and passing just fine

describe "project creation" do
  before(:each) do
    @project = FactoryGirl.create(:project)
  end
  it "create projects fluently" do
    visit root_path
    @project.should validate_presence_of(:title)
  end
end

Then I installed spring, and when I run spring rspec spec/features/projects_spec.rb, it throws

Failure/Error: @project.should validate_presence_of(:title)
     NoMethodError:
       undefined method `validate_presence_of' for #<RSpec::Core::ExampleGroup...

How can it be

Baldrick
  • 23,882
  • 6
  • 74
  • 79
user1611830
  • 4,749
  • 10
  • 52
  • 89
  • Are you including the shoulda-matchers gem? validate_presence_of is a matcher from that gem... – guilleva Apr 09 '14 at 21:30
  • @guilleva indeed I do not, because it is working fine when not using `spring`. Btw, I have just added this and it doesn't change anything ! – user1611830 Apr 09 '14 at 21:33
  • spring preforks processes. this might cause issues when code get's reloaded. class variables and monkeypatching are especially sensistive to this kind of stuff. have a look at spring after_fork hook. that might help you in some kind of way. overall, it's hard to say without looking at the whole app. you need to find out where those methods come from and why the preforked process does not have them. – phoet Apr 10 '14 at 01:52

1 Answers1

1

This issue was discussed here: https://github.com/rails/spring/issues/209, but in the readme file of shoulda-matchers gem say how install it with a preloader, basically my solution was:

  1. Move the rspec-rails line in Gemfile above the shoulda-matchers

  2. Add require: :false to shoulda-matchers in Gemfile

  3. Add require 'shoulda/matchers' in spec_helper.rb

My Gemfile look like:

group :test do
  gem "rspec-rails"
  gem 'shoulda-matchers', require: false
  gem 'machinist'
end

Now my specs with shoulda helper work fine with spring!

Armando
  • 940
  • 7
  • 16