Skip to content

Instantly share code, notes, and snippets.

@jsgarvin
Last active August 29, 2015 13:57
Show Gist options
  • Save jsgarvin/9573255 to your computer and use it in GitHub Desktop.
Save jsgarvin/9573255 to your computer and use it in GitHub Desktop.
Name your MiniTest::Mock for more descriptive errors.
MiniTest::Mock errors are especially vague, particularly when you need more than one mock in your test.
To improve things slightly, add this to the bottom of your test_helper...
module MiniTest
class NamedMock < Mock
attr_reader :name
def initialize(name)
@name = name
super()
end
def method_missing(sym, *args, &block)
super(sym, *args, &block)
rescue NoMethodError, MockExpectationError, ArgumentError => error
raise(error.class, "#{error.message} (mock:#{name})")
end
end
end
Then use NamedMock instead of Mock...
let(:mock_foobar) { MiniTest::NamedMock.new('foobar') }
let(:mock_shazam) { MiniTest::NamedMock.new('shazam') }
And, Voilà...
NoMethodError: unmocked method :frozen?, expected one of [] (mock:shazam)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment