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
Related
Can I define all of my stub methods at one place instead of defining them in each test case?
For example i have this piece of code
SocialNetworks::Facebook.any_instance.stubs(:update_status).returns(true)
in multiple test cases. But if I move it in test_helper.rb file then running test cases won't work.
Does mocha gem allowed to place these stubs at one place?
I am using rails Minitest for testing rails app. And mocha gem for
stubing and mocking.
You can do that in setup callback, like:
class YourTest < ActiveSupport::TestCase
setup do
SocialNetworks::Facebook.any_instance.stubs(:update_status).returns(true)
end
end
setup is called before each test case.
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.
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.
I have a helper class, ApplicationHelper, that has a method, build_links(). I have another class, AppleClass, that refers to that method.
AppleClass
def foo
....
build_links
end
end
ApplicationHelperClass
def build_links
main_app.blah_path(1)
end
end
The complication here is that there's an Engine, so I usually explicitly reference "main_app.blah_path" not just "blah_path".
The test against foo passes by itself, in its file, and when I run all helpers. It fails, though, when I include it in all the unit tests - "rake spec:suite:unit", and with our entire suite. All Apple tests pass, all ApplicationHelper tests pass. The only failing ones are when one method is referring to the other method, in routes, outside of the engine, in the full suite.
`undefined local variable or method `main_app' for #
<RSpec::Core::ExampleGroup::Nested_45::Nested_1:0x007fc134b30130>`
My suspicion is that the test helper, or some config, is not loading the engine's routes early enough, and thus links to "main_app" don't make sense. If I remove main_app, the test fails until it's run in the main suite.
Does anyone have tips on troubleshooting what's really going on? Also, could I kickstart the routing somehow in test_helper?
ruby-1.9.3-p385, rails 3.2.13, rspec 2.13.0
I had the same issue and found that if I added this method to the top of my Controller RSpec test case, then it resolved the issue entirely.
def main_app
Rails.application.class.routes.url_helpers
end
I think this issue is related to this question
In my RSpec controller test, I'm trying to test a particular action that makes a call to a subfunction. But I don't want the test to include the call to that subfunction. (it'll raise an error that simply cannot be fixed in my dev environment)
What's the best way to omit this subfunction call from this controller action while I'm running a test over it?
Thanks.
You can stub that subfunction call in the before block of your rspec test for that function like so
describe "test for the main_action"
before(:each) do
controller.stub!(:sub_action).returns(true)
end
end
Then none of your test examples would actually call this sub_action.
Just for semantics, Rspec always runs in test environment not in development but I gather what you mean.