我正在进行单元测试,我正在尝试测试包含Promise
的组件方法,在执行promise后我重定向用户。我想测试路径路径,因此我使用fakeAsync
和tick
等待承诺完成但我收到此错误:
TypeError:'instanceof'的右侧不是对象
我真的不知道出了什么问题,我做的与文档完全相同。
以下是组件:
@Component({
selector: 'login',
styles: [ require('./_login.component.scss') ],
template: require('./_login.component.html'),
})
export class LoginComponent {
public loginForm = {
password: '',
username: '',
}
constructor (
private loginService: LoginService,
private router: Router
)
public submitLoginForm () {
this.loginService.log(this.loginForm.username, this.loginForm.password)
.then((value) => {
this.router.navigateByUrl('/home')
})
}
}
这是测试
describe('Component: LoginComponent', () => {
let component: LoginComponent
let fixture: ComponentFixture<LoginComponent>
let loginMockService: LoginService
let location: Location
beforeEach(() => {
TestBed.configureTestingModule({
declarations: [
LoginComponent,
MockComponent,
],
imports: [ NxtModule, RouterTestingModule.withRoutes([{ component: MockComponent, path: 'home' }]) ],
providers: [
{ provide: 'AppStore', useValue: appStore },
{ provide: LoginService, useValue: loginService },
],
})
fixture = TestBed.createComponent(LoginComponent)
component = fixture.componentInstance
fixture.detectChanges()
loginMockService = fixture.debugElement.injector.get(LoginService)
location = TestBed.get(Location)
})
it('should log', fakeAsync(() => {
spyOn(loginMockService, 'log').and.callThrough()
component.loginForm.username = 'john.doe@test.com'
component.loginForm.password = 'password'
component.nxtForm.updateForm('username', component.loginForm.username, true)
component.nxtForm.updateForm('password', component.loginForm.password, true)
component.submitLoginForm()
expect(loginMockService.log).toHaveBeenCalled()
tick()
expect(location.path()).toEqual('/home')
}))
})
如果我删除了fakeAsync
和tick
但它无法测试expect(location.path()).toEqual('/home')
,我已经尝试了一切:删除node_modules和pacakge-lock.json并运行npm i
,将Angular更新为v5.0.5等等。
谢谢