Menu

Assert throws AND doesn't affect?

Help
Zog Bog
2010-03-10
2012-10-22
  • Zog Bog

    Zog Bog - 2010-03-10

    I just found a case where I want to check that something throws an error AND
    that it doesn't affect the results. For example, let's say I have a function
    that slaps three numbers onto the end of a list, but only if they're in order,
    and if they're not in order, it shouldn't add any on, not even ones that come
    before the first that's out of order.

    For example (pseudocode):

    blah.putNumbers 5, 10, 20
    Assert.That blah.getNumbers, Iz.EqualTo(5, 10, 20)
    
    blah.putNumbers 100, 101, 110
    Assert.That blah.getNumbers, Iz.EqualTo(5, 10, 20, 100, 101, 110)
    
    Assert.Throws E_FAIL
    blah.putNumbers 120, 15, 140
    

    But I don't want only to check that an error is thrown; I also want to make
    sure that the implementation didn't add 120 to the list before it realized
    that 15 was out of order.

    Is there a way to do this? Check both that an error is thrown, and that some
    condition is true afterwards?

    Thanks in advance.

     
  • Kelly Ethridge

    Kelly Ethridge - 2010-03-10

    Instead of using Assert.Throws in this scenario you could trap the error
    yourself and handle additional assertions.

        On Error Goto errTrap
        blah.putNumbers 120, 15, 140
        Assert.Fail "An error should be raised."
    
    errTrap:
        Assert.That Err.Number, Iz.EqualTo(E_FAIL), "Wrong error number raised."
    
        ' add additional asserts to check the state of blah.
    
        Assert.Pass
    
     
  • Zog Bog

    Zog Bog - 2010-03-10

    Thanks!

     

Log in to post a comment.