在用茉莉花返回诺言的方法上运行测试案例
我正在为返回承诺的功能写一个测试用例。
newItem: any;
public getNewItem() {
this.storage.get('newItem').then((response) => {
if (response) {
this.newItem = response
} else {
this.doReset();
}
}
当我运行测试时,所有doreset都被称为“ newitem的原因”。我如何设置新项目,以免运行。
这是规格文件:
describe('ItemService', () => {
let service: ItemService;
beforeEach(() => {
TestBed.configureTestingModule({
imports: [HttpClientModule,RouterTestingModule, IonicStorageModule.forRoot()],
providers: [
{provide: AlertService}
],
});
service = TestBed.inject(ItemService);
});
it('should be current item', () => {
service.newItem = {name: 'Daniel', photos: ['image1.jpg', 'image2.jpg']}
spyOn(service,'getCurrentItem').and.callThrough();
});
});
I'm writing a test case for a function that returns a promise.
newItem: any;
public getNewItem() {
this.storage.get('newItem').then((response) => {
if (response) {
this.newItem = response
} else {
this.doReset();
}
}
When i run the test all doReset get called the reason being newItem is not being assigned. How do i set new item so doReset doesn't run.
This is the spec file:
describe('ItemService', () => {
let service: ItemService;
beforeEach(() => {
TestBed.configureTestingModule({
imports: [HttpClientModule,RouterTestingModule, IonicStorageModule.forRoot()],
providers: [
{provide: AlertService}
],
});
service = TestBed.inject(ItemService);
});
it('should be current item', () => {
service.newItem = {name: 'Daniel', photos: ['image1.jpg', 'image2.jpg']}
spyOn(service,'getCurrentItem').and.callThrough();
});
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有不同的方法来编写 asynchronous tests 带有
jasmine
。就您而言,最简单的方法是使用async
/等待
,如下:There exist different ways to write asynchronous tests with
Jasmine
. In your case, the easiest way would be to useasync
/await
as follows: