Alex Rivera | Logout

How do I stub things in MiniTest?

Asked 2011-08-26T22:15:48.933
77

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().returns("War and Peace")

Then whenever I call @book.title it returns "War and Peace".

Is there a way to do this within MiniTest? If yes, can you give me an example code snippet?

Or do I need something like mocha?

MiniTest does support Mocks but Mocks are overkill for what I need.

Edit
Report

1 Answer

30

I use minitest for all my Gems testing, but do all my stubs with mocha, it might be possible to do all in minitest with Mocks(there is no stubs or anything else, but mocks are pretty powerful), but I find mocha does a great job, if it helps:

require 'mocha'    
Books.any_instance.stubs(:title).returns("War and Peace")
answered 2011-08-28T07:18:09.537

Your Answer