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