【问题标题】:How to test an asynchronous function using karma/Jasmine in ionic2?如何在ionic2中使用业力/茉莉花测试异步函数?
【发布时间】:2023-03-26 18:45:02
【问题描述】:

下面我写了一个异步函数的代码,它在内部使用 setTimeout() 并在 3 秒后打印消息。

我必须使用 Jasmine 编写规范。

我已关注docs,但我不知道如何在代码中应用它。

home.ts

//implementing only function written in a class.All imports are done properly.

click(){

function delayedAlert(message: string, time: number, cb) {
      return setTimeout(() => cb(message), time);
    }
    delayedAlert('Aditya3', 3000, (message) => console.log(message));//function is called
  }
}

home.spec.ts

describe('Asynchronous call', () => {
    let fixture: ComponentFixture<HomePage>;
    let comp: HomePage;
    let de: DebugElement;
    let el: HTMLElement;


    beforeEach(async(() => {
        TestBed.configureTestingModule({
            declarations: [HomePage],
            imports: [
                IonicModule.forRoot(HomePage),
            ],
            providers: [NavController],
        }).compileComponents();
    }));

    beforeEach(() => {
        fixture = TestBed.createComponent(HomePage);
        comp = fixture.componentInstance;
        de = fixture.debugElement

    });

it('Asynchronous testing with callback', ()=>{
         //how to write the spec here for the asynchronous function wrriten in the above class using Karma/Jasmine.
})
});

在测试规范中标注的cmets中,我需要写规范。

【问题讨论】:

    标签: angular ionic2 jasmine angular2-testing


    【解决方案1】:

    【讨论】:

    • 在这篇文章 jasmine.async 库中,他们使用的是 async.it 等,你能帮我在不使用 jasmine.async 库的情况下编写这个规范吗?
    • 我无法实现它。你能帮忙吗?
    猜你喜欢
    • 2014-12-12
    • 1970-01-01
    • 2017-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-27
    • 2019-12-30
    • 2018-01-23
    相关资源
    最近更新 更多