0

I started messing around with jasmine but I'm having trouble with this code:

describe('UserService with errorsServiceSpy and testbed', () => {
    //how to test service that depends by another service
    let userService: UserService;
    let errorsServiceSpy: jasmine.SpyObj<ErrorsHandlerService>;

    beforeEach(() => {
        const spy = jasmine.createSpyObj('ErrorsHandleService', ['getErrors', 'addError']);

        TestBed.configureTestingModule({
            providers: [
                UserService, { provide: ErrorsHandlerService, useValue: spy }
              ]
        });

        userService = TestBed.inject(UserService);
        errorsServiceSpy = TestBed.inject(ErrorsHandlerService) as jasmine.SpyObj<ErrorsHandlerService>;
    });

    it('should be created', () => {
        expect(userService).toBeTruthy();
    });

    it('should return user', () => {
        expect(userService.getUser).toBe(userService.user)
    })

    it('#getErrors should return #error value from a spy', () => {
        const error:string = 'prova errore';
        userService.generateError(error)
        let errors:any = errorsServiceSpy.getErrors.toString()
        console.log('errors', errors)
        expect(errorsServiceSpy.getErrors.toString()).toContain(error)
    });

})

I state that without testBed or with testBed without spy I can test. But when I use the spy errorsServiceSpy.getErrors.toString () which should return an array in string, it returns a function: '

function() {
    return fn.apply(this, arguments);
}

look at all the code in the stackblitz!
pay no attention to the failed test due to 'this.errorsService' I think it's a stackblitz issue

Why it return me a function? How to test the service's method? WHit testBed and spy?
if you need more details I am at your complete disposal

Liam
  • 27,717
  • 28
  • 128
  • 190
CaneRandagio
  • 366
  • 2
  • 15
  • It's very unclear what your trying to achieve here, do you want to know if `errorsServiceSpy.getErrors` has been invoked in the code your testing? – Liam Aug 27 '20 at 15:16
  • That's because your spy has nothing to do with original implementation and all those methods are mocked – yurzui Aug 27 '20 at 15:17
  • @Liam yes it is invoked nad return a function – CaneRandagio Aug 27 '20 at 15:24
  • @yurzui that's what I assumed ... so the spy has nothing to do with the service! It's useless? – CaneRandagio Aug 27 '20 at 15:25
  • No it's not useless @AndreaDiCioccio, [it's a mock](https://stackoverflow.com/questions/2665812/what-is-mocking). It allows you to inject dependencies and to test implementation details. – Liam Aug 27 '20 at 15:27

1 Answers1

1

You're thinking about it wrong, you don't want to get the errors, you want to know if your method has been invoked with a particular parameter, so:

let errors:any = errorsServiceSpy.getErrors.toString()
expect(errorsServiceSpy.getErrors.toString()).toContain(error)

Should be:

expect(errorsServiceSpy.getErrors).toHaveBeenCalledWith(error);

Though you don't actually show what getErrors does so the actual implementation will vary. I'd suggest you read the docs

Also please be consistent with the semi colons in your code

Liam
  • 27,717
  • 28
  • 128
  • 190
  • I was following angular's guide on unit tests. the test I would like to do is verify that getErrors actually returns me the errors generated. – CaneRandagio Aug 27 '20 at 15:33
  • That doesn't make sense, `errorsServiceSpy` is a spy, it only returns what you tell it to return. You don't test spies/mocks, the spies/mocks are hard coded pieces of functionality to allow you to test other things. If you want it to actually do something you have to inject the real implementation of this test, not a spy. – Liam Aug 27 '20 at 15:34
  • ho corretto il test suggerito in expect(errorsServiceSpy.addError).toHaveBeenCalledWith(error) e funziona – CaneRandagio Aug 27 '20 at 15:35
  • lool forgive me! a slip! I understand what you want to tell me ... it's not the test I have to do ... – CaneRandagio Aug 27 '20 at 15:36
  • in fact if you look at the code in the stackblitz there are other tests with the service itself injected. ok! thank you – CaneRandagio Aug 27 '20 at 15:38
  • Niente, in bocca al lupo – Liam Aug 27 '20 at 15:48