Call rspec directly on model results for stub errors! method

At startup:

rake spec:models 

everything works fine but when i do

 rspec spec/models/spot_spec.rb 

which has Spot.stub! :test1 Spot.stub! :test1 , I get:

 undefined method `stub!' for Spot:Class 

The error only happens when I turn on this stub! line.

Any ideas how to avoid this? I want to run specifications only for a specific model.

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 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 
+4
source share
2 answers

Disabled as issue in a call to before(:all) :

It is right. Mocks is implicitly checked and cleared after (: each) so they won’t work before (: all).

Changing it to before(:each) , he decided.

Thanks to everyone.

+14
source

Ask spot_spec.rb to include spec_helper.rb, and then make sure spec_helper.rb includes spot_spec.rb.

If you are using ruby ​​1.9+, you can use require_relative to include spot_spec.rb in spec_helper.rb

Update:

in spec_helper.rb add:

 require_relative '../app/models/spot' 
+1
source

Source: https://habr.com/ru/post/1337585/


All Articles