Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No provider for TestComponentBuilder #81

Closed
mbakker96 opened this issue Apr 21, 2016 · 2 comments
Closed

No provider for TestComponentBuilder #81

mbakker96 opened this issue Apr 21, 2016 · 2 comments
Labels

Comments

@mbakker96
Copy link
Contributor

Hi

My project tests files are almost the same as the clickers project so I think this question can be asked here. (correct me if I'm wrong)

I want to do the same as in the clickerlist.spec.ts file. I want to fake a class because when I create a new settingsService class settingsService = new settingsService() it triggers the constructor and I want to mock that.

This is what I saw in your code and I want to to do that also in my class.

But I get the error Failed: No provider for TestComponentBuilder! But don't know where to add that provider and could not find where you did it

This is my unit test what I've so far:

import {WrittenHoursPage}       from './written-hours';
import {SettingsService}        from '../../services/settings-service';
import {provide}                from 'angular2/core';
import {
    beforeEach,
    beforeEachProviders,
    ComponentFixture,
    injectAsync,
    TestComponentBuilder,
}                               from 'angular2/testing';
import {Utils} from '../../services/utils';

let writtenHoursPage: WrittenHoursPage        = null;
let writtenHoursPageFixture: ComponentFixture = null;

class MockClass {
    public getSelectedDate(): Date {
        return new Date(2016, 3, 15);
    }
}

export function main(): void {
    'use strict';

    describe('WrittenHours', () => {
        beforeEachProviders(() => [
            provide(SettingsService, {useClass: MockClass}),
        ]);

        beforeEach(injectAsync([TestComponentBuilder], (tcb: TestComponentBuilder) => {
            return tcb
                .createAsync(WrittenHoursPage)
                .then((componentFixture: ComponentFixture) => {
                    writtenHoursPageFixture = componentFixture;
                    writtenHoursPage        = componentFixture.componentInstance;
                    writtenHoursPageFixture.detectChanges();
                })
                .catch(Utils.promiseCatchHandler);
        }));

        it('initialize', () => {
            expect(writtenHoursPage).not.toBeNull();
        });

    });
}
@lathonez
Copy link
Owner

https://github.com/lathonez/clicker/blob/master/app/app.spec.ts#L6-L7

Also in the blog:

Note the setBaseTestProviders line, enabling us to utilise Angular 2’s testing framework in our tests.

You should have a read of this blog

@lathonez
Copy link
Owner

#191

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants