I think you want assert_raises , which claims that the block / expression passed to it will throw an exception at startup.
For example, one of my projects has the following minitest:
test 'attempting to create a CreditCard that fails to save to the payment processorshould prevent the record from being saved' do assert_equal false, @cc.errors.any? failure = "simulated payment processor failure" failure.stubs(:success?).returns(false) failure.stubs(:credit_card).returns(nil) PaymentProcessor::CreditCard.stubs(:create).returns(failure) assert_raises(ActiveRecord::RecordNotSaved) { create(:credit_card) } end
What does this mean in my case:
- Create a simulated error message from the payment document API
- Block the creation of a credit card on the payment processor to return a simulated failure.
- Try to create a credit card, simulating that the payment processor returned a failed status, and claim that my internal save / create method generates an error in these conditions.
I have to say that this test code includes things in addition to minitest, such as FactoryGirl, and (I think) should and mocha. In other words, what is shown above is not a strictly minimal code.
source share