简体   繁体   中英

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 ?

尝试这个:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM