UnitTestClass - acadet/oscar GitHub Wiki

Defining a test class is really simple. Main step is to extend UnitTestClass.

In brief

class EngineTest extends UnitTestClass {
    
    constructor() {
        super();
        
        // Called only once
    }

    secretMethod() : void {
        // Oscar does not deal with this method
    }

    setUp() : void {
        // Called before each test
    }

    tearDown() : void {
        // Called after each test
    }

    firstTest() : void {
        // Autorun thanks to 'test' keyword'
    }

    firstAsyncTest(obs : IOscarObserver) : void {
        // Autorun thanks to 'asyncTest' keyword
    }
}

References

setUp()

Defining a setting up method is optional. However, if you do it, this method will be run before each test.

tearDown()

Like setUp(), a cleaning method is optional. If an implementation is present, it will be triggered after each test.

Define a simple test

It is really painless. First of all, write your test method as you wish. Then, you just have to end its name by test keyword to be taken on board by Oscar.

Write an asynchronous test

As clear as a simple test. Just end its name by asynctest keyword this time.

Also, Oscar provides you an argument for this kind of test: IOscarObserver. You have to use it to set your async test as successful or failed at its end.

anAsyncTest(obs : IOscarObserver) : void {
    asyncMethod(
        // ... args
        (outcome) => {
            // on complete
            obs.success();
        },
        (error) => {
            // on failure
            // error is optional
            obs.fail(error);
        }
    );
}

Notes

  • Keywords are insensitive.
  • For async tests, tearDown() is run after observer having been notified. Besides, tests are run one by one.
  • About async tests, their runtime is limited. Mind they are considered as failed after this period. For more details, see TestSuite section.