Error message test on minitest

I use mini_test for testing. I have a piece of code as shown below.

raise Type_Error, 'First Name must not be empty' if @person.first_name == nil 

How can I write this test code? Thanks...

+4
source share
2 answers
 raise Type_Error, 'First Name must not be empty' if @person.first_name == nil 

For testing above the line, I wrote a test as shown below. For this, I used minitest :: spec.

 def test_first_name_wont_be_nil person.name = nil exception = proc{ Context.new(person).call }.must_raise(TypeError) exception.message.must_equal 'First Name must not be empty' end 

Context is the place where some process is running.

+1
source

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.

+2
source

All Articles