Testing TypeScript Classes with Jasmine

I would like to ask you how I can test a class that accepts an constructor instance of another class. For example, I want to test the hasChildRoutes () method:

    class Route implements ng.route.IRoute {
    public url: string;
    public config: RouteConfig;

    constructor(url: string, config: RouteConfig) {
                this.url = url;
                this.config = config;
            }

    public hasChildRoutes(): boolean {
                return this.config.childRoutes.length > 0;
            }
}

I wrote a bad unit test for this (they create new instances of other classes, which, in my opinion, are bad):

 beforeEach(() => {
        routeSetting = new RouteSetting(1, '');
        routeConfig = new RouteConfig('', '', routeSetting, [], '');
    });


    describe('Methods test', () => {
        var childRoute: Route;

        beforeEach(() => {
            route = new Route('', routeConfig);
        });

        it('sould return false when Route has no child routes', () => {
            expect(route.hasChildRoutes()).toBeFalsy();
        });

        it('sould return true when Route has child routes', () => {
            routeConfig = new RouteConfig('', '', routeSetting, [route], '');
            route = new Route('', routeConfig);

            expect(route.hasChildRoutes()).toBeTruthy();
        });
    });
+4
source share
2 answers

It is true for you to provide dependency instances as part of the arrangement under the / act / assert agreement.

, a ( b) , mock b . sinonjs: http://sinonjs.org/

+2

.

, beforeEach afterEach . . :

it('should return false when Route has no child routes', () => {
// arrange
routeSetting = new RouteSetting(1, '');
routeConfig = new RouteConfig('', '', routeSetting, [], '');
// act
route = new Route('', routeConfig);
// assert
expect(route.hasChildRoutes()).toBeFalsy();
});

, hasChildRoutes() RouteConfig? ?

0

All Articles