How do I stub things in MiniTest?

前端 未结 8 877
面向向阳花
面向向阳花 2020-12-02 09:46

Within my test I want to stub a canned response for any instance of a class.

It might look like something like:

Book.stubs(:title).any_instance().ret         


        
8条回答
  •  一整个雨季
    2020-12-02 10:30

    You cannot stub all instances of a class, but you can stub any instance method of a given object like this:

    require "minitest/mock"
    
    book = Book.new
    book.stub(:title, 'War and Peace') do
      assert_equal 'War and Peace', book.title
    end
    

提交回复
热议问题