无论我在异步断言中的期望是什么,测试总是通过。此外,在console.log
语句后调用then
时,不会显示任何日志。我的代码如下所示..
import { TestBed, async } from '@angular/core/testing'
import { RouterTestingModule } from '@angular/router/testing'
import { HttpModule, XHRBackend } from '@angular/http'
import { MockBackend } from '@angular/http/testing'
import { ApiService } from '../../client/services/api.service '
import { SocketService } from '../../client/services/socket.service'
import { EventService } from '../../client/services/event.service'
import { DirectMessageComponent } from '../../client/components/messages/direct.component'
import { SharedModule } from '../../client/components/common/shared.module'
describe('DirectMessageComponent', () => {
// High order variables
let comp
let fixture
// Asynchronous beforeEach
beforeEach(async(() => {
// Configure the test bed
TestBed.configureTestingModule({
imports: [ SharedModule, HttpModule, RouterTestingModule ],
declarations: [ DirectMessageComponent ],
providers: [
ApiService,
SocketService,
EventService,
{ provide: XHRBackend, useClass: MockBackend }
]
})
.compileComponents() // compile template and css
}))
// Synchronous beforeEach
beforeEach(() => {
// Assignments
fixture = TestBed.createComponent(DirectMessageComponent)
comp = fixture.componentInstance
// Detect changes which fires ngOnInit
fixture.detectChanges()
})
it('should get messages', async(() => {
fixture.whenStable()
.then(() => {
expect(comp.message.length).toBeGreaterThan(0)
})
}))
})
有趣的是,即使正确调用fakeAsync
,使用tick()
方法执行也会失败。