考虑这个角度分量:
export class CheckoutComponent {
constructor(private paymentService: PaymentService, private paymentModalService: PaymentModalService) {}
public onCheckout(): void {
const paymentStatus$: Observable<string> = this.paymentService.getStatus();
const paymentRequired$ = paymentStatus$.pipe(map(paymentStatus => paymentStatus === 'INCOMPLETE'));
paymentRequired$.subscribe(() => {
this.paymentModalService.open();
});
}
}
我正在尝试编写一个茉莉花测试来确认它paymentModalService.open()
被称为:
import { cold } from 'jasmine-marbles';
describe('CheckoutComponent', () => {
let component: CheckoutComponent;
let mockPaymentService: jasmine.SpyObj<PaymentService>;
let mockPaymentModalService: jasmine.SpyObj<PaymentModalService>;
beforeEach(() => {
mockPaymentService = jasmine.createSpyObj(['getStatus']);
mockPaymentModalService = jasmine.createSpyObj(['open']);
component = new CheckoutComponent(mockPaymentService, mockPaymentModalService);
});
it('should open payment modal if required', () => {
mockPaymentService.getStatus.and.returnValue(cold('a', { a: 'INCOMPLETE' }));
component.onCheckout();
expect(mockPaymentModalService.open).toHaveBeenCalled(); // FAIL: was never called
});
});
然而,显然从未调用过 open 方法。
在断言该方法已被调用之前,有什么方法可以等待可观察的冷测试完成发射?
我试过使用fakeAsync
,tick
但似乎也没有帮助。我意识到它可以of()
代替 a 使用TestColdObservable
,但我希望对可观察行为进行更精细的控制。我真的不打算完成getStatus()
observable。