package asunit.runners { import asunit.errors.AssertionFailedError; import asunit.framework.TestCase; import asunit.framework.IResult; import asunit.framework.Result; import asunit.framework.TestFailure; import flash.events.Event; public class TestRunnerExpectsErrorTest extends TestCase { private var runner:TestRunner; private var successTest:Class; private var throwNothingTest:Class; private var throwWrongErrorTest:Class; public function TestRunnerExpectsErrorTest(testMethod:String = null) { super(testMethod); } protected override function setUp():void { super.setUp(); runner = new TestRunner(); successTest = TestExpectsArgumentErrorAndThrowsIt; throwNothingTest = TestExpectsArgumentErrorButThrowsNothing; throwWrongErrorTest = TestExpectsArgumentErrorButThrowsWrongError; } protected override function tearDown():void { super.tearDown(); runner = null; successTest = null; throwNothingTest = null; throwWrongErrorTest = null; } public function test_method_expects_specific_error_and_throws_it_yields_successful_test_result():void { runner.addEventListener(Event.COMPLETE, addAsync(check_Result_has_no_errors, 100)); runner.run(successTest); } private function check_Result_has_no_errors(e:Event):void { var runnerResult:IResult = runner.bridge; assertEquals('no errors in testResult', 0, runnerResult.errorCount); assertEquals('no failures in testResult', 0, runnerResult.failureCount); } public function test_method_expects_specific_error_but_nothing_thrown_yields_assertion_failure():void { runner.addEventListener(Event.COMPLETE, addAsync(check_Result_has_one_assertion_failure, 100)); runner.run(throwNothingTest); } private function check_Result_has_one_assertion_failure(e:Event):void { var runnerResult:IResult = runner.bridge; assertFalse(runnerResult.wasSuccessful); assertEquals('one failure in testResult', 1, runnerResult.failureCount); assertEquals('no errors in testResult', 0, runnerResult.errorCount); var failure0:TestFailure = runnerResult.failures[0] as TestFailure; assertTrue('thrownException is correct type', failure0.thrownException is AssertionFailedError); assertTrue('failedTest is instance of the test class', failure0.failedTest is throwNothingTest); assertSame('failedMethod name', 'fail_by_throwing_nothing', failure0.failedMethod); } public function test_method_expects_specific_error_but_wrong_one_thrown_yields_assertion_failure():void { runner.addEventListener(Event.COMPLETE, addAsync(check_Result_has_one_assertion_failure, 100)); runner.run(throwNothingTest); } } } class TestExpectsArgumentErrorAndThrowsIt { [Test(expects="ArgumentError")] public function throwArgumentError():void { throw new ArgumentError('generated by TestExpectsArgumentError'); } } class TestExpectsArgumentErrorButThrowsNothing { [Test(expects="ArgumentError")] public function fail_by_throwing_nothing():void { } } class TestExpectsArgumentErrorButThrowsWrongError { [Test(expects="ArgumentError")] public function fail_by_throwing_wrong_error():void { throw new Error('generated by TestExpectsArgumentErrorButThrowsWrongError'); } }