Eric Francis
Eric Francis

Reputation: 24267

NoMethodError: undefined method `stub' for ModuleX:Module

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:

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

Answers (3)

Honza
Honza

Reputation: 4409

for me this was caused by not adding require 'minitest/mock' to my test_helper.rb

Upvotes: 5

gdurelle
gdurelle

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

gdurelle
gdurelle

Reputation: 2031

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.

Upvotes: 2

Related Questions