添加新测试后单元测试失败

时间:2018-08-30 14:08:09

标签: unit-testing jasmine karma-jasmine angular2-testing

我目前正在开发Ionic(v3)应用程序,并且我们进行了一些测试来测试服务,页面和组件。

一切正常,直到我为组件添加了新测试。

两个测试都可以单独正常运行(如果以fdescribe开始,或者我将其中之一注释掉)。

测试如下:

verify-key.spec.ts

describe('Component: VerifyKey', () => {

  let component: VerifyKeyComponent
  let fixture: ComponentFixture<VerifyKeyComponent>

  beforeEach(() => {
    TestBed.configureTestingModule({
      declarations: [VerifyKeyComponent],
      imports: [
        IonicModule.forRoot(VerifyKeyComponent)
      ]
    })

    // create component and test fixture
    fixture = TestBed.createComponent(VerifyKeyComponent)

    // get test component from the fixture
    component = fixture.componentInstance
  })
  ...
})

wallet-select-coins.spec.ts

describe('Wallet-Select-Coin Component', () => {

  let fixture: ComponentFixture<WalletSelectCoinsPage>
  let component: WalletSelectCoinsPage

  beforeEach(async(() => {
    TestBed.configureTestingModule({
      declarations: [WalletSelectCoinsPage],
      imports: [
        IonicModule.forRoot(WalletSelectCoinsPage),
        ComponentsModule,
        IonicStorageModule.forRoot({
          name: '__airgap_storage',
          driverOrder: ['localstorage']
        })
      ],
      providers: [
        SecretsProvider,
        {
          provide: SecureStorageService,
          useFactory: SecureStorageFactory,
          deps: [Platform]
        },
        { provide: NavController, useClass: NavControllerMock },
        { provide: NavParams, useClass: NavParamsMock },
        { provide: StatusBar, useClass: StatusBarMock },
        { provide: SplashScreen, useClass: SplashScreenMock },
        { provide: Platform, useClass: PlatformMock }
      ]
    })
  }))

  beforeEach(() => {
    fixture = TestBed.createComponent(WalletSelectCoinsPage)
    component = fixture.componentInstance
    fixture.detectChanges()
  })

  it('should not show hd-wallet dropdown if currency does not support it', () => {
    let el = fixture.debugElement.nativeElement
    let ethereumRadio = el.querySelector('#eth')

    // click on ethereum
    ethereumRadio.click()
    fixture.detectChanges()
    console.log(component.selectedProtocol)
    expect(component.selectedProtocol).toBeDefined() // This fails
    expect(component.selectedProtocol.identifier).toEqual('eth')

    // eth should not show hd wallets
    let hdWalletSelector = el.querySelector('#wallet-type-selector')
    expect(hdWalletSelector).toBeFalsy()
  })

})

如果两个测试均启用,则第二个测试在第expect(component.selectedProtocol).toBeDefined()行失败,错误为Expected undefined to be defined.

如果我在第一个文件的第fixture = TestBed.createComponent(VerifyKeyComponent)行中注释掉,那么第二个测试将运行而没有任何问题。

我的第一个想法是在第一个测试中以某种方式修改了TestBed。因此,我尝试在第一次测试后添加TestBed.resetTestingModule(),但这并没有任何改变。

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

编写测试时,最好的做法是在每次测试后清理所有内容,以便所有测试可以随机顺序运行而不会影响其他测试。

我建议在每次测试中引入afterEach()afterAll(),以清除产生的任何数据。因为大多数测试环境都在同一上下文中运行所有测试。

为什么您希望将TestBed.configureTestingModule创建为async()?您的测试将运行,但是异步部分可能永远不会在it(..

之前被调用

希望这些想法会有所帮助:)