Unit testing 使用document.location测试函数
我需要有关使用document.location的测试操作的帮助。 我正在运行react/redux+sinon/mocha。 以下是我的行动:Unit testing 使用document.location测试函数,unit-testing,reactjs,mocha.js,sinon,redux,Unit Testing,Reactjs,Mocha.js,Sinon,Redux,我需要有关使用document.location的测试操作的帮助。 我正在运行react/redux+sinon/mocha。 以下是我的行动: export function importFile(file) { return (dispatch) => { dispatch(importFile()); return jQuery.post('/api/import/', {file}).done((data) => {
export function importFile(file) {
return (dispatch) => {
dispatch(importFile());
return jQuery.post('/api/import/', {file}).done((data) => {
window.location = `/edit/${data.id}`;
}).fail((err) => {
return dispatch(importFileError(err));
});
};
}
如果您在命令行中运行我的测试-我无法检查在完成后是否有重定向到页面的操作,但如果我在浏览器中运行我的测试,我会重定向,并且所有其他测试都会被删除。
这是我的测试:
it('should create an action to upload import file', (done) => {
const id = '123';
const file = 'testname';
const server = sinon.fakeServer.create();
const expectedActions = [
{type: actions.IMPORT_FILE}
];
const store = mockStore({}, expectedActions, done);
store.dispatch(actions.importFileUpload(file));
server.respondWith([201, { 'Content-Type':'application/json' }, `{"id":"${id}","version":12}`]);
window.XMLHttpRequest = sinon.useFakeXMLHttpRequest();
server.respond();
});
测试这样的函数的最佳方法是什么?防止重定向或使用重定向函数的包装器?
谢谢。我用Mocha测试了一段相当复杂的代码,为了将它与Mocha隔离开来,我在iframe
中运行它。这可以防止我的应用程序弄乱摩卡测试。它还可以防止分配到窗口。位置
停止测试,因为窗口。位置
是帧的本地位置。(框架中的窗口
对象与包含框架的页面中的对象不同。)我用摩卡测试了一段相当复杂的代码,为了将其与摩卡分离,我在iframe
中运行它。这可以防止我的应用程序弄乱摩卡测试。它还可以防止分配到窗口。位置
停止测试,因为窗口。位置
是帧的本地位置。(框架中的窗口
对象与包含框架的页面中的对象不同。)