public
Created

Example of problem with rspec 2 + rails 3.1 + ruby 1.9.2 + factory_girl

  • Download Gist
Gemfile
Ruby
1 2 3 4 5 6 7 8 9 10
gem 'rails', '3.1.0.rc4'
 
group :development, :test do
gem 'rspec-rails', '~> 2.6.1'
end
 
group :test do
gem 'factory_girl_rails'
gem 'rspec'
end
other_model.rb
Ruby
1 2 3 4 5 6 7 8 9
class OtherModel < ActiveRecord::Base
after_create :some_slow_method, :on => :create
private
def some_slow_method
# do something that takes a while
end
end
other_model_spec.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11
require 'spec_helper'
 
describe OtherModel do
before(:each) do
@other_model = Factory.create :other_model
end
it "should work right" do
@other_model.true_thing.should be_true
end
end
shell output.txt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
stephan$ ruby -v ; rails -v ; rspec -v
ruby 1.9.2p180 (2011-02-18 revision 30909) [x86_64-darwin10.7.0]
Rails 3.1.0.rc4
2.6.4
 
stephan$ rspec spec/models/some_model_spec.rb spec/models/other_model_spec.rb
.F
 
Failures:
 
1) Comment
Failure/Error: @other_model = Factory(:other_model)
NameError:
undefined local variable or method `some_slow_method' for #<OtherModel:0x00000106ff72e0>
# ./spec/models/comment_spec.rb:5:in `block (2 levels) in <top (required)>'
 
Finished in xx.xx seconds
2 examples, 1 failure
 
stephan$ rspec spec/models/other_model_spec.rb spec/models/some_model_spec.rb
..
 
Finished in xx.xx seconds
2 examples, 0 failures
some_model_spec.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13
require 'spec_helper'
 
describe SomeModel do
before(:each) do
OtherModel.any_instance.stub(:some_slow_method)
 
@some_model = Factory.create :some_model
end
it "should handle some_model normally" do
@some_model.true_thing.should be_true
end
end

LOTS of detail has been omitted here. I've experimented with lots of different settings in my gemfile, including working straight from master. The terminal output is faked, based on my real output, to illustrate the issue.

seems like it may be related to https://github.com/rspec/rspec-mocks/issues/54

also tried unstubbing manually in an after(:all) or after(:each) block, wonder if it's related to this as well: https://github.com/rspec/rspec-mocks/pull/68

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.