I am trying execute test cases with rspec version 2.14 for which I am getting following error
undefined method `rspec_reset' for
I am trying to use rspec_reset on the class. The same test cases are working with rspec 2.13.1. So is it possible that rspec_reset method is not available after 2.13?
The reset method does not exist in RSpec 2.14.x. Instead, it is a helper method defined inside the spec_helper.rb file for the rspec-mocks project.
module VerifyAndResetHelpers
def verify(object)
RSpec::Mocks.proxy_for(object).verify
end
def reset(object)
RSpec::Mocks.proxy_for(object).reset
end
end
You can see that this method delegates the reset action to the underlying proxy instead of tacking it on to the class definition of the object in question.
Yes, in 2.14, rspec_reset is no longer available on all objects as it was previously, as discussed in https://github.com/rspec/rspec-mocks/pull/250.
Although I can't find any documentation on it, there now appears to be an RSpec class method reset which takes an object as an argument and will effectively "undo" any RSpec operations that have been done to that object.
There's an RSpec "example" at https://github.com/rspec/rspec-mocks/blob/cee433c89125a3984df33c87eb61985613adce9b/spec/rspec/mocks/mock_spec.rb which still uses the rspec_reset in the description of the example, but which now uses the aforementioned reset method to do the reset. In earlier versions of the example, the reset was done with rspec_reset.
Related
I had a code in rspec 2.14.1 like
allow_any_instance_of(AnyClass).to receive(:some_method).and_call_original
with a corresponding message expectation
expect_any_instance_of(AnyClass).to receive(:some_method)
The above worked fine in rspec 2.14.1. After upgrade to rspec 3.1.0, the above code no longer works. It fails on the message expectation that some_method is not called even once
However if I change the stub like
allow_any_instance_of(AnyClass).to receive(:some_method).and_return(value)
it works fine in rspec 3.1.0.
I just wanted to understand why using and_call_original with allow_any_instance_of fails after rspec upgrade.
I can see from this link that and_call_original is only supported on partial doubles.
Does that mean using allow_any_instance_of is not partial double?
and_call_original does actually work when used with allow_any_instance_of.
Refer the specs for any_instance which gives us an idea of different ways of mocking or to un-stub using and_call_original here.
To answer my question above, the way I was using message expectation is wrong. It should be:
allow_any_instance_of(AnyClass).to(
receive(:any_method).and_call_original
)
expect(AnyClass.new.any_method).to eq(:any_method_value)
I was trying to use expect_any_instance_of instead of expect which caused the issue.
My controller class inherits from ActionController::Base.
I am using rails 2 and ruby 1.9.3.
I am using Mocha to stub out calls to Perforce so my tests are not dependent on connections to perforce.
In my rails after_filter, I call one of my stubbed methods, hoping that it has not yet been unstubbed. In the normal application, it releases the Perforce connections.
I get this error message:
unexpected invocation: Api::BuildsController.perforce_service()
The method perforce_service is the method I stubbed out. I suspect that it is being called after it has already been unstubbed by Mocha, but I am not sure. When is the Mocha unstubbing done relative to the after_filter? How should I architect this?
When I try manually stubbing and unstubbing an object's method in pry, the method is still callable after I call unstub, which is not what I expect. I expect that unstub will restore the original method, or if it had no original method, to restore it to a state of being an undefined method. What does it really do?
NOTE:
Mocha documentation says to put this in test_helper.rb if you use rails:
require 'mocha/mini_test'
However, if I add this to my Gemfile and run bundle install:
gem 'mocha/mini_test'
then I get an error:
Could not find gem 'mocha/mini_test (>= 0) ruby' in the gems available on this machine
What am I doing wrong here?
I've seen this done using the older should_receive syntax, but I can't figure out how to stub a Rails module method in Rspec 3.
I've tried these:
OptionsHelper.stubs(:method).returns("something")
allow(OptionsHelper).to receive(:method).and_return "something"
allow_any_instance_of(ActionView::Base).to receive(:render_quick_help).and_return 'something'
But none actually stub the method.
allow_any_instance_of(ActionView::Base)
Means it will expect instance of that exact class and not classes that inherit from it to receive :method.
Also ActionView::Base is a class, ActionView:: is a module.
Furthermore, Modules can not be instantiated, so in theory allow_any_instance_of(ActionView).to ... will have no effect.
I know it's not a solution, but should point you in right direction.
Using Mocha, I am trying to mock a controller method that calls a module method.
This is for an integration test.
Example:
class Controller < ApplicationController
def method1
response = Module1.method2(...
My steps so far:
Added mocha to gemfile
Added require 'mocha/mini_test' to the very bottom of my
test_helper.rb
Tried this code in my integration test before sending a post to my controller:
Module1.stub(:method2).returns(:true)
post "controller/method1"
And got this error:
NoMethodError: undefined method 'stub' for Module1:Module
Is it possible to stub method2?
EDIT: So the main fix is that the method is 'stubs' not 'stub'. I'm still having trouble mocking this dang Module though.
EDIT: Rails and MiniTest just call the module method even after I've stubbed it. Is it possible that Rails is overwriting my stub?
class Test < ActionDispatch::IntegrationTest
test "test do
Module1.stubs(:method2).returns(:true)
post "controller/method1"
This test leads to error inside method2 bc no parameters were passed in. The test is behaving as if the method was not stubbed.
try .stubs with an s.
The stubnotation is to build a stub you'lle use later on.
Add an s when you stub a method directly on something.
regarding your test you may want a
Module1.expects(:method2)
but the stub should work anyway. Rails would not overrite your stub, maybe you test_helper does, but i'm quite sure it is more a syntax thing.
Paste your real code and test because here right know it's kind of difficult and can be anything... a callback preveting the method to be called, the test syntax, ...
for me this was caused by not adding require 'minitest/mock' to my test_helper.rb
I have installed a rspec-rails 3.0.0.beta1 (on ruby2 + rails4) and had some troubles using devise helper in my request specs. After some googling, I've found that I need to move all my specs from spec/requests to spec/features (the requests dir was created by rspec installator or scaffold generator [not sure right now], so I'm a bit confused). That made my devise helper working but there are more issues instead.
Here are three scenarios:
Spec file is spec/requests/events_spec.rb and dont have any type set
undefined method 'visit' for #<RSpec::ExampleGroups::Events::GETEvents:0x007ff2464d9848>
Spec file is spec/requests/events_spec.rb and has a type: :controller
it throws an error undefined method 'events_path' for nil:NilClass when i'm trying to use a get events_path method(s)
Spec file is spec/features/events_spec.rb and dont have any type set
undefined method `get' for #<RSpec::ExampleGroups::Events::GETEvents:0x007ffb2714a968>
Spec file is spec/features/events_spec.rb and have a type: :controller
undefined method `events_path' for nil:NilClass
I think I can find some tweaks on the internet but I'm a fresh rspec user and I feel like I'm doing something extremely wrong. And all the examples online are not related to my problem.
The code is here: https://gist.github.com/mbajur/8002303
As of Capybara 2.0, the Capybara methods (e.g. visit) are only available by default for feature specs, not request specs or controller specs.
Similarly, the get method is not available for feature specs, only controller and request specs.
Finally, the path helper methods are only available in request or feature specs.
Given that, your failures can be explained as follows:
No Capybara, because it's not a feature spec.
No path helper methods because it's a controller spec.
No get method because it's a feature spec.
No path helper method because it's a controller spec.
You need to either:
Make it a request spec and configure RSpec to include Capybara::DSL for request specs
Leave it a feature spec and stop using methods like get which aren't intended for use with feature specs
Here's some interesting background on introducing feature specs as distinct from request specs