【发布时间】:2016-11-29 01:22:09
【问题描述】:
我正在使用酶、sinon 并希望对我的反应组件进行单元测试。
import React from 'react';
import expect from 'expect.js';
import { shallow } from 'enzyme';
import ExampleComponent from './../../../src/js/components/example-component';
describe('Test <ExampleComponent />', function() {
beforeEach(function() {
this._sandbox = sinon.sandbox.create();
this.constructorSpy = this._sandbox.spy(ExampleComponent.prototype, 'constructor');
});
afterEach(function() {
this._sandbox.restore();
this.constructorSpy = null;
});
it('Should set the state with the correct data [constructor]', function() {
const wrapper = shallow(<ExampleComponent />);
console.log(' - callCount: ', this.constructorSpy.callCount);
expect(this.constructorSpy.calledOnce).to.be(true);
expect(Immutable.is(wrapper.state('shownData'), Immutable.Map())).to.be(true);
});
我的组件构造函数中有一些逻辑,它根据我作为道具传入的内容来设置状态。但是,这个测试一直告诉我构造函数调用计数为 0,并且没有被调用。
监视组件构造函数的正确方法是什么?我做错了什么?
我正在使用沙盒,因为我想将其他功能添加到沙盒中以供将来监视。
【问题讨论】:
标签: javascript unit-testing reactjs sinon expect.js