Skip to main content
September 28, 2009
Question

"expectnot" or equivalent

  • September 28, 2009
  • 3 replies
  • 2798 views

I was wondering if we have something for automating an assertion for a specific error that should not occur, rather than the test failing. This would be the opposite of expects="Error...". This may sound like an edge case, however I find that from time to time I like to test that a specific error has not occured, and it would be nice to do so outside of a try..catch block.

Perhaps there may be something already in place that I am not aware of?

Best,

Eric

This topic has been closed for replies.

3 replies

October 2, 2009

Just a nice to have which can be implemented if needed via an extension.

Participating Frequently
September 28, 2009

Eric,

So, pondering your use case for a moment. Are you basically saying you want to be able to say 'this test will throw an error, and that is okay, so long as it isn't error x'

If not, clarify for me.

Thanks,

Mike

September 28, 2009

This is really just semantics to help provide additional information about a test, as, on occasion I have found it helpful to convey intent as to why a test passed, rather than the test passing simply being enough. This is really just to state intent

For instance, suppose I have an method which throws a specific error, and I want to test the method to ensure that specific error is not thrown. Of course I could just write a test as follows:

[Test]

public function testSomeOperationDoesntThrowSomeError() : void

{

     someobject.someoperation(...);

}

However, this kind of test feels somewhat awkward as it looks like an orphaned test or something to that extent. I could leave a comment or something, but that would not help with any test output etc.

I could modify this test to:

[Test]

public function testSomeOperationDoesntThrowSomeError() : void

{

     var someError:SomeError = null;

     try {

           someobject.someoperation(...);

     }

     catch(error:SomeError)

     {

          someError = error;

     }

     Assert.assertNotNull("Expecting operation have have thrown 'SomeError', someError );

}

What I think would be helpful would be to have framework support which complements "expects" with a "expectnot" (or something to that extent) which would allow for the above test to be rewrite to this effect:

[Test(expectnot="SomeError")

public function testSomeOperationDoesntThrowSomeError() : void

{

      someobject.someoperation( validParameter );

}

[Test(expects="SomeError")

public function testSomeOperationDoesntThrowSomeError() : void

{

     someobject.someoperation( invalidParameter );

}

The metadata states the intent, so the test no longer feels like some arbitrary call that could be testing any number of things, but rather it clearly states exactly what is being tested. Addmitedly this is just semantics, or may even just be a moot point, however I wanted to mention it.

Hope that clarifies.

Best,

Eric

Participating Frequently
October 1, 2009

Fair enough. Honestly, you could add that today as FlexUnit preserves any metadata put into those tags, even the stuff it doesn't act on.

So, if there is no functional requirement on it, then it works today

Known Participant
September 28, 2009

If the test method finished successfully, then by definition, your exception didn't occur. Isn't that enough?

Known Participant
September 28, 2009

For example, if I successfully drive to  work tomorrow, then FlatTireError and OutOfGasError did not occur.