如何在茉莉花的afterEach函数中调用describe或它的函数
How to call describe or it function in afterEach function in jasmine
我想在每个 it
功能后检查按钮禁用状态。所以我使用 afterEach
函数,但发生了如下错误。
如何在 afterEach 函数或任何解决方法中调用 describe
或 it
函数。
'it' should only be used in 'describe' function
describe('Input Error Check', () => {
const fixture = TestBed.createComponent(AppComponent);
afterEach( () => {
const searchButon = fixture.debugElement.query(By.css('#searchButton')).nativeElement as HTMLButtonElement
expect(searchButon.disabled).toBeTruthy()
})
it('No Input Value', () =>{
// write test code
})
it('Invalid Input Value', () =>{
// write test code
})
})
afterEach
方法仅用于拆卸逻辑。 The jasmine docs about it say:
Run some shared teardown after each of the specs in the describe in
which it is called.
您可以在每个测试的末尾添加这个期望,如下面的代码所示:
function withSearchButtonDisabled(testCallback) {
return () => {
testCallback();
const searchButon = fixture.debugElement.query(By.css('#searchButton')).nativeElement as HTMLButtonElement
expect(searchButon.disabled).toBeTruthy()
}
}
describe('Input Error Check', () => {
const fixture = TestBed.createComponent(AppComponent);
it('No Input Value', withSearchButtonDisabled(() =>{
// write test code
}))
it('Invalid Input Value', withSearchButtonDisabled(() =>{
// write test code
}))
})
PS我没有测试代码,可能有错别字
我想在每个 it
功能后检查按钮禁用状态。所以我使用 afterEach
函数,但发生了如下错误。
如何在 afterEach 函数或任何解决方法中调用 describe
或 it
函数。
'it' should only be used in 'describe' function
describe('Input Error Check', () => {
const fixture = TestBed.createComponent(AppComponent);
afterEach( () => {
const searchButon = fixture.debugElement.query(By.css('#searchButton')).nativeElement as HTMLButtonElement
expect(searchButon.disabled).toBeTruthy()
})
it('No Input Value', () =>{
// write test code
})
it('Invalid Input Value', () =>{
// write test code
})
})
afterEach
方法仅用于拆卸逻辑。 The jasmine docs about it say:
Run some shared teardown after each of the specs in the describe in which it is called.
您可以在每个测试的末尾添加这个期望,如下面的代码所示:
function withSearchButtonDisabled(testCallback) {
return () => {
testCallback();
const searchButon = fixture.debugElement.query(By.css('#searchButton')).nativeElement as HTMLButtonElement
expect(searchButon.disabled).toBeTruthy()
}
}
describe('Input Error Check', () => {
const fixture = TestBed.createComponent(AppComponent);
it('No Input Value', withSearchButtonDisabled(() =>{
// write test code
}))
it('Invalid Input Value', withSearchButtonDisabled(() =>{
// write test code
}))
})
PS我没有测试代码,可能有错别字