Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Unit testing 摩卡咖啡&x2B;Axios+;信农+;柴单元测试_Unit Testing_Mocha.js_Sinon_Chai_Enzyme - Fatal编程技术网

Unit testing 摩卡咖啡&x2B;Axios+;信农+;柴单元测试

Unit testing 摩卡咖啡&x2B;Axios+;信农+;柴单元测试,unit-testing,mocha.js,sinon,chai,enzyme,Unit Testing,Mocha.js,Sinon,Chai,Enzyme,我有一个react组件,其函数如下,它使用axios调用api服务。我想知道如何使用Ezyme、sinon和chai编写单元测试 import React from 'react'; import es6BindAll from 'es6bindall'; import { browserHistory } from 'react-router'; import axios from 'axios'; export default class Header extends React.Comp

我有一个react组件,其函数如下,它使用axios调用api服务。我想知道如何使用Ezyme、sinon和chai编写单元测试

import React from 'react';
import es6BindAll from 'es6bindall';
import { browserHistory } from 'react-router';
import axios from 'axios';

export default class Header extends React.Component {
    constructor() {
        super();
        es6BindAll(this,['handleLogoutClick']);
    }
    handleLogoutClick(e) {
        e.preventDefault();
        let that = this;
            this.serverRequest = axios({
                url: 'Url_to_call',
                method: 'post'
            }).then(function (successData) {
                browserHistory.push("nextPage to navigate");                
            }.bind(that));
    }
    render(){
        return(
            <div className="columns large-12 header-container">
                <h6 className="logout" onClick={this.handleLogoutClick}>Logout</h6>
                </div>
            </div>
        )
    }
}
请让我知道如何测试axios和mock browserHistory。我没有在任何浏览器上运行测试,因此请让我知道如何模拟


谢谢

您需要按如下方式使用sinon来存根
浏览历史记录

import { browserHistory } from 'react-router';
import { stub } from 'sinon';
const browserHistoryPushStub = stub(browserHistory, 'push', () => { });

对于模拟axios请求,您可以使用

请更具体一些。你有没有寻找并尝试过一些解决方案?你遇到了什么问题?
var jsdom = require('jsdom').jsdom;

global.document = jsdom('');
global.window = document.defaultView;
Object.keys(document.defaultView).forEach((property) => {
  if (typeof global[property] === 'undefined') {
    global[property] = document.defaultView[property];
  }
});
global.window.loggedInUserData = {userName: 'TestName'};
global.navigator = {
  userAgent: 'node.js'
};
import { browserHistory } from 'react-router';
import { stub } from 'sinon';
const browserHistoryPushStub = stub(browserHistory, 'push', () => { });