Javascript ngX异步断言未获得正确的预期
无论我在异步断言中期望什么,测试总是通过的。此外,在调用Javascript ngX异步断言未获得正确的预期,javascript,unit-testing,jasmine,Javascript,Unit Testing,Jasmine,无论我在异步断言中期望什么,测试总是通过的。此外,在调用控制台时,不会显示日志。在然后语句之后,会显示log。我的代码如下所示 import { TestBed, async } from '@angular/core/testing' import { RouterTestingModule } from '@angular/router/testing' import { HttpModule, XHRBackend } from '@angular/http' import { MockB
控制台时,不会显示日志。在然后语句之后,会显示log
。我的代码如下所示
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()
,它也会失败