我正在爲nightwatch.js的單頁應用程序編寫e2e測試。如何使用nightwatch.js和sinon.js來模擬API響應?
我有一些API請求,如驗證。所以我想用sinon.js的fakeServer來模擬響應數據。這是我的代碼。
import sinon from 'sinon';
const WAIT_TIME = 5000;
const host = 'http://localhost:3000/#/';
const uri = new RegExp(escape('/users/login'));
module.exports = {
'Login Test': function(browser) {
let server;
browser
.windowSize('basicTest', 1440, 710)
.url(host + 'account/login')
.waitForElementVisible('body', WAIT_TIME)
.setValue('input[type=email]', '[email protected]')
.setValue('input[type=password]', 'password')
.execute(function() {
server = sinon.fakeServer.create();
server.respondWith('POST', uri, [
200, { 'Content-Type': 'application/json' }, JSON.stringify(someResponseData),
]);
})
.submitForm('form')
.execute(function() {
server.respond();
})
.waitForElementNotPresent('input.[type=submit]', WAIT_TIME) // the page should be redirected to another page
.execute(function() {
server.restore();
server = null;
})
.end();
},
};
我不能模擬響應,並得到了下面的錯誤(當API服務正在運行,有沒有錯誤,但反應不會被嘲笑一個)。
Error: Origin is not allowed by Access-Control-Allow-Origin
我想知道,首先,使用sinon.js的fakeServer是否正確?這是可能的e2e(和nightwatch.js)?
請給我一個幫助。
我已經決定要使用的RESTify ... https://github.com/restify/node-restify – okmttdhr