Angular 角度:测试返回可见光的防护装置
我试图测试一个auth-guard,它返回一个可观察的,并调用另一个函数。我的后卫看起来像这样(摘自托德的座右铭。托德干杯): 在执行此规范时,我在Karma中遇到了以下错误: TypeError:无法读取未定义的属性“indexOf” 位于Function.TestScheduler.parseMarbles(./node_modules/rxjs/_esm5/internal/testing/TestScheduler.js?:243:21) 我错过了什么?我不想只为这一种方法使用大理石测试,但如果有人能建议一种方法,我很乐意尝试 来自NgRx的示例代码使用toBeObservable显示,因此:Angular 角度:测试返回可见光的防护装置,angular,unit-testing,jasmine,rxjs,karma-runner,Angular,Unit Testing,Jasmine,Rxjs,Karma Runner,我试图测试一个auth-guard,它返回一个可观察的,并调用另一个函数。我的后卫看起来像这样(摘自托德的座右铭。托德干杯): 在执行此规范时,我在Karma中遇到了以下错误: TypeError:无法读取未定义的属性“indexOf” 位于Function.TestScheduler.parseMarbles(./node_modules/rxjs/_esm5/internal/testing/TestScheduler.js?:243:21) 我错过了什么?我不想只为这一种方法使用大理石测试
const expected = hot('-------a', {
a: {
type: '[Customers API] Search Customers Success',
customers: [...],
},
});
expect(
effects.searchCustomers$({
debounce: 20,
scheduler: getTestScheduler(),
})
).toBeObservable(expected);
.toBeObservable()
需要一个testhotoobservable
或TestColdObservable
,它们具有marbles
属性。我之所以来到这里,是因为我遇到了一个TypeError错误:无法使用.toBeObservable()
读取未定义的属性“marbles”,而不带任何参数
如果您想使用非TestXXXObservable
类型的可观察对象进行测试,您必须订阅它们并在订阅中验证结果,如图所示:
这里是一个完全的黑暗拍摄,但是如果您将expect
行更改为result.subscribe(res=>expect(res).toEqual(true))
它有效吗?我很好奇,因为.toBeObservable()匹配器来自marble测试库,可能他们的代码有问题。我看到了同样的情况!你在@serlingpa找到解决方案或解决方法了吗?这是因为expectedObservable
从到可观察(expectedObservable)
应该是一个TestObservable
(TestColdObservable;testhotoobservable
)具有marbles
属性。您需要使用cold()
或hot()
函数来创建一个@尼克达维不知道你在说什么:/你能写一个答案并补充一些具体的细节吗?先生,你是个天才!我用的是()而不是冷热。Thx很多。如果我是个天才,我就不会犯错误写这篇文章,但很高兴它帮助了别人:)
it('should return true when checkStore() returns true', () => {
spyOn(guard, 'checkStore').and.returnValue(of(true));
const result = guard.canActivate();
expect(result).toBeObservable(of(true));
});
const expected = hot('-------a', {
a: {
type: '[Customers API] Search Customers Success',
customers: [...],
},
});
expect(
effects.searchCustomers$({
debounce: 20,
scheduler: getTestScheduler(),
})
).toBeObservable(expected);
// create an actions stream and immediately dispatch a GET action
actions$ = of({ type: '[Customers Page] Get Customers' });
// mock the service to prevent an HTTP request
customersServiceSpy.getAllCustomers.and.returnValue(of([...]));
// subscribe to the Effect stream and verify it dispatches a SUCCESS action
effects.getAll$.subscribe(action => {
expect(action).toEqual({
type: '[Customers API] Get Customers Success',
customers: [...],
});
});