Rspec view undefined method stub_model
What is your rspec-rails version?
According to the Changelog and this Commit mock_model
and stub_model
are removed since version 3.0.0 of rspec-rails.
rspec mocks are externalized in an another gem rspec-activemodel-mocks . You should include it in your Gemfile and try it.
Hope it helps
rspec mocking undefined method `stub_model' for #Class:0x007ff9c339bd80 (NoMethodError)
See comment in original question. Like a bad knot in my neck, it seems to have worked itself out.
Rspec View test model stub variable not passing to the view for loop
RSpec's stub_model
expects a constant as an argument such as an ActiveRecord::Base
subclass used to define a model. It also expects either additional parameters or a block in order to set the values of the instance of the class.
You've passed in an ActiveModel::Relation
, which is the result of a the all
, and even if you'd provided the expected class constant, you haven't provided any parameter values.
See the examples for how this is intended to be used at https://www.relishapp.com/rspec/rspec-rails/v/2-3/docs/mocks/stub-model
stub_model deprecated rspec
See https://en.wikipedia.org/wiki/Deprecation for a definition of deprecation. You get rid of deprecation warnings by no longer using the feature in question. Usually, the deprecation warning suggests an alternative approach to what you are trying to do.
In the case of stub_model
, the advice was to include a gem, since the functionality was going to be removed from the main RSpec software but would continue to be available in that gem.
In the case of pending
, the need for a feature (passing a block) was eliminated, so continued use of that feature made no sense. See http://rspec.info/blog/2014/05/notable-changes-in-rspec-3/#changes-to-pending-semantics-and-introduction-of-skip for more information on this issue and see the larger article for more discussion of deprecation-inducing changes.
Stub Model Save Method in Rspec/Rails
If I'm not mistaken, Alliance.stub(:save)
would affect calls to Alliance.save
. You want @alliance.stub(:save).and_return(true)
.
Mocha has a useful method any_instance
, so you could do something like Alliance.any_instance.stubs(:save).returns(true)
, which would (as the name implies) stub the save
method for any instance of Alliance
.
Rails/Rspec - How to stub a model method inside of a controller test?
allow(Tasks).to receive(:assign_random_test?).and_return(true)
RSpec.describe TaskController, type :controller do
describe '#next' do
before do
allow(Tasks).to receive(:assign_random_test?).and_return(true)
end
context 'when assign_random_test' do
it 'should return result' do
#your test here
end
end
context 'when not assign_random_test' do
before do
allow(Tasks).to receive(:assign_random_test?).and_return(false)
end
it 'should return result' do
#your test here
end
end
end
end
Rspec 3 upgrade issues with view.stub (Rails)
allow(view).to receive_messages(current_user: nil)
Notable Changes in RSpec 3
issue with stubs and rspec old syntax
Do it like this instead:
allow(hand).to receive(:cards) { cards }
https://github.com/rspec/rspec-mocks#method-stubs
Related Topics
How to Get Generators Call Other Generators in Rails 3
Rails 4 Update Nested Attributes
Rails: Update Model Attribute Without Invoking Callbacks
How to Deal with Ruby 2.1.2 Memory Leaks
How to Properly Chain Custom Methods in Ruby
How to Select the Longest String from a Ruby Array
Testing Hash Contents Using Rspec
Differencebetween 'File.Read' and 'Io.Read'
Trying to Understand Use of Self.Method_Name VS. Classname.Method_Name in Ruby
Rbenv: Ruby: Command Not Found
Sending a Delete Request from Sinatra
How to Transfer Files Using Ssh and Scp Using Ruby Calls
Running Webrick Server in Background