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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With