【问题标题】:How to call describe or it function in afterEach function in jasmine如何在茉莉花的afterEach函数中调用describe或it函数
【发布时间】:2020-12-19 03:36:27
【问题描述】:

我想在每个it 函数之后检查按钮禁用状态。所以我使用了afterEach 函数,但出现了如下错误。
如何在 afterEach 函数或任何 workarround 中调用 describeit 函数。

'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
    })
    
  })

【问题讨论】:

    标签: javascript angular unit-testing jasmine


    【解决方案1】:

    afterEach 方法仅用于拆卸逻辑。 The jasmine docs about it say:

    在描述中的每个规范之后运行一些共享拆解 它被称为。

    您可以在每个测试的末尾添加这个期望,如下面的代码所示:

    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我没有测试代码,可能有错别字

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-06-29
      • 1970-01-01
      • 1970-01-01
      • 2015-09-12
      • 1970-01-01
      • 2012-06-12
      • 2016-08-24
      相关资源
      最近更新 更多