Reputation: 24267
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:
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.
Upvotes: 6
Views: 6123
Reputation: 4409
for me this was caused by not adding require 'minitest/mock'
to my test_helper.rb
Upvotes: 5
Reputation: 2031
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, ...
Upvotes: 1
Reputation: 2031
try .stubs
with an s.
The stub
notation is to build a stub you'lle use later on.
Add an s when you stub a method directly on something.
Upvotes: 2