1

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 ?

Vega
  • 27,856
  • 27
  • 95
  • 103
user1611830
  • 4,749
  • 10
  • 52
  • 89
  • Your original code is the `rspec` syntax, not `mocha` syntax – Uri Agassi Apr 08 '14 at 11:23
  • @UriAgassi in fact if I implement the above response and I replace `stub` by `stubs`, it works. How is it so ? and what is in my code `rspec` type code since it can work with the indicated modifications ? – user1611830 Apr 08 '14 at 11:33
  • 1
    Maybe I confused you with the word "syntax". I meant API. `stub`, `and_return` are APIs of the `rspec` library. `stubs` and `return` are part of the `mocha` API. (@MarekLipka had a typo in his answer) – Uri Agassi Apr 08 '14 at 11:39

1 Answers1

3

Try this:

@mymodel.stubs(:method).returns(true)
Marek Lipka
  • 50,622
  • 7
  • 87
  • 91