Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

  • 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.

like image 325
Eric Francis Avatar asked Oct 21 '22 11:10

Eric Francis


1 Answers

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.

like image 106
gdurelle Avatar answered Oct 29 '22 14:10

gdurelle