Fok*_*ker 5 javascript unit-testing browser-history react-router ava
我为我的行为编写测试,即使用
{ browserHistory } from 'react-router';
Run Code Online (Sandbox Code Playgroud)
当我运行我的测试时,导入的browserHistory由于未知原因未定义.因此,测试会抛出一个错误 - "无法读取属性'推送'的未定义"; 我不知道,为什么browserHistory未定义,如果它在我的应用程序中工作.有人能帮助我吗?
use*_*091 14
我猜你没有使用业力或任何浏览器来运行你的测试.如果没有浏览器,则对象browserHistory将是未定义的.您可能需要使用sinon来存储您的browserHistory.以下内容可能有用:
import chai from 'chai';
import sinonChai from 'sinon-chai';
import componentToTest from './component-to-test'
import sinon from 'sinon';
import * as router from 'react-router';
var expect = chai.expect;
chai.use(sinonChai);
describe('Test A Component', () => {
it('Should success.', () => {
router.browserHistory = { push: ()=>{} };
let browserHistoryPushStub = sinon.stub(router.browserHistory, 'push', () => { });
//mount your component and do your thing here
expect(browserHistoryPushStub).to.have.been.calledOnce;
browserHistoryPushStub.restore();
});
});
Run Code Online (Sandbox Code Playgroud)