ruby-on-railsrubytestingruby-mocha

stub is not defined while testing


Suppose, I am running this test

Class MyModelTest < ActiveSupport::TestCase  
  def setup do
    @mymodel = MyModel.new
  end

  @mymodel.stub(:method).and_return { true }

but I get :undefined method 'stub' for nil:NilClass. I thought it was related to the nil instance, in fact when I dump with @mymodel with data, I get the same message undefined method stub'. Then I tried @mymodel.stubs(:method).and_return { true }, and I got this message

undefined method `and_return'

How can I solve that ?


Solution

  • Try this:

    @mymodel.stubs(:method).returns(true)