I'm trying to set up tests of my @Injectable
storage service.
@Injectable()
export class StorageService {...
constructor(private platform: Platform,
public instanceService: InstanceService,
private logger: LoggingService,
private database: SQLite = null) {... //injecting a db mock
I've created several versions of the test that succeed, but I've learned they only succeed because they are async and are misreporting the error.
Here's the setup.
describe('Storage Service tests', () => {
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [],
providers: [
StorageService,
{ provide: Platform, useClass: PlatformMock },
{ provide: InstanceService, useClass: InstanceServiceMock },
{ provide: LoggingService, useClass: LoggingServiceMock },
{ provide: ConfigurationService, useClass: ConfigurationServiceMock },
{ provide: SQLite, useClass: DBMock },
MockBackend,
BaseRequestOptions,
{
provide: Http,
useFactory: (mockBackend, options) => {
return new Http(mockBackend, options);
},
deps: [MockBackend, BaseRequestOptions]
}
],
imports: [
HttpModule
]
}).compileComponents();
}));
Here I'm trying to test setFailedImageUpdate
I'm struggling to know how to both inject the service so it initializes and pass in done
for the async completion.
it('should set failed image', (done) => {
let cu = new UnsavedImageUpdate();
inject([StorageService, MockBackend], (storageService, mockBackend) => {
storageService.db = new DBMock();
storageService.setFailedImageUpdate([cu]).then((res) => {
expect(res).toEqual("1234");
done();
})();
})
});
The error I'm getting on this iteration is:
Error: Timeout - Async callback was not invoked within timeout
specified by jasmine.DEFAULT_TIMEOUT_INTERVAL.
How can I set up my test to both inject StorageService
and pass in done
?
EDIT: Trying to follow @learner's suggestions, I created this:
it('should set failed image', async() => {
let cu = new UnsavedImageUpdate();
var storageService = TestBed.get(StorageService);
storageService.db = new DBMock();
storageService.setFailedImageUpdate([cu]).then((res) => {
expect(res).toEqual("1234");
})
});
The test succeeds, but when I debug into it, I got this error:
'expect' was used when there was no current spec,
this could be because an asynchronous test timed out
Instead of using inject
, you can get the injected service in your test method using TestBed.get(StorageService)
and you can use async
function from angular testing module which waits for the async operation to complete instead of using done
.
Use done like this:
it('should set failed image', (done) => {
let cu = new UnsavedImageUpdate();
var storageService = TestBed.get(StorageService);
storageService.db = new DBMock();
storageService.setFailedImageUpdate([cu]).then((res) => {
expect(res).toEqual("1234");
done();
})
});
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments