SimonTestNet / SimonTest

Repository for SimonTest questions and issues https://simontest.net
16 stars 2 forks source link

makes expected calls always fail #10

Closed Tuizi closed 7 years ago

Tuizi commented 7 years ago

On each test generated, the makes expected calls always fail. Here an example:

app.component.ts

export class AppComponent implements OnInit {
  title = 'i works!';

  constructor(private toto: TotoService) {
  }

  ngOnInit(): void {
    this.title = this.toto.get();
  }
}

app.component.spec.ts

    describe('ngOnInit', () => {
        it('makes expected calls', () => {
            spyOn(totoServiceStub, 'get');
            comp.ngOnInit();
            expect(totoServiceStub.get).toHaveBeenCalled();
        });
    });

Also if I do:

      spyOn(totoServiceStub, 'get').and.callFake(() => {
        debugger;
        console.debug('toto');
      });

The debugger is never called and I don't see 'toto' in the console either

Tuizi commented 7 years ago

Ok so it works if in the generated test I do this changes:

Generated tests from Simon

    describe('ngOnInit', () => {
        it('makes expected calls', () => {
            spyOn(totoServiceStub, 'get');
            comp.ngOnInit();
            expect(totoServiceStub.get).toHaveBeenCalled();
        });
    });

Fixed tests

  describe('ngOnInit', () => {
    it('makes expected calls', () => {
      // Get injected service
      const totoServiceStubInjected = fixture.debugElement.injector.get(TotoService) as any;

      spyOn(totoServiceStubInjected, 'get');
      comp.ngOnInit();
      expect(totoServiceStubInjected.get).toHaveBeenCalled();
    });
  });

Do you think that it's something that should be automatically made by the extension?

ManuelDeLeon commented 7 years ago

You just gave me a good reason for using fixture.debugElement.injector.get. I'll update the generator to use that.

ManuelDeLeon commented 7 years ago

Sorry for the delay. Pick up v0.12.0