When running:
rake spec:models
everything works well, but when I do
rspec spec/models/spot_spec.rb
that has Spot.stub! :test1
, I get:
undefined method `stub!' for Spot:Class
The error happens only when I include that stub! line.
Any ideas how to avoid it? I want to run the specs for a specific model only.
Update:
Using Ruby 1.9.2 and RSpec 2.4.0, here is the spot_spec.rb code:
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
describe Spot do
before(:all) do
Spot.stub! :test1
@spot = Spot.new
end
subject {@spot}
describe "validations" do
it { should validate_presence_of(:user) }
end
end
And the spec_helper.rb:
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
RSpec.configure do |config|
config.mock_with :rspec
end
Turned out to be an issue in before(:all)
call:
That's correct. Mocks are implicitly verified and cleared out after(:each) so they won't work in before(:all).
Changing it to before(:each)
solved it.
Thanks everyone.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With