我正在为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