lundi 19 septembre 2016

Angular2 "Final" Unit Test Override Directive

I am using Angular 2 Final, and using the TestBed for Unit tests

I have an issue when when using "overrideDirective" it seems that the actual directive is being called, as I am getting an error "No provider for Config!" which MyDirective that was replaced is dependent on.

Component View

  <div>
       <input myDirective>
       // some more logic etc...
  </div>

Directive to Replace

@Directive({
    selector: "myDirective"
})
class MyDirective{
    constructor(
        private config: Config
    ) {
    }
}

Spec File

@Component({
    template: "<my-component></my-component>"
})
class TestComponent{

}

@Directive({
    selector: "fake"
})
class FakeDirective {

}

beforeEach(() => {
    TestBed.configureTestingModule({
        declarations: [
            MyComponent,
            MyDirective,
            TestComponent,
            FakeDirective
        ]
    });
});

beforeEach(async(() => {
    TestBed
        .overrideDirective(MyDirective, FakeDirective)
        .compileComponents();
    fixture = TestBed.createComponent(TestComponent);
}));

Any one encountered the issue and managed to fix it please?

Thanks

Aucun commentaire:

Enregistrer un commentaire