我需要帮助来了解如何为这段代码设计测试:
onSuccessHandler(response) {
if (response && this.cartModel.hasData(response)) {
const data = this.cartModel.parse(response);
this.cartModel.set(data);
cartChannel.request('render:cart');
}
}
我尝试了一下,看看请求是否在购物车频道上被调用了,它似乎正在传递,但是代码覆盖率仍然表明上面的代码没有被覆盖:)。
describe('Cart suite', () => {
let view;
const cartChannel = Radio.channel('cart');
beforeEach(() => {
view = new CartWidgetView({
model: new Model(fixturesObject),
cartModel,
});
view.render();
});
it('should request cart if response has data', () => {
//spyOn Channel in beforeEach method
view.cartModel = {
hasData: () => true,
parse: () => true,
set: (data) => {
view.data = data;
},
};
view.onButtonClick();
view.render();
const response = {};
cartChannel.trigger('render:cart', cartModel);
view.onSuccessHandler(response);
expect(cartChannel.request).toHaveBeenCalled();
expect(cartChannel.request).toHaveBeenCalledWith('get:model');
});
});
我刚开始使用Jasmine和MarionetteJS来进行TDD。目的是使它涵盖在测试范围内。