I try mocking HTTP requests with nock and other libs like sinonjs but without success.
import nock from "nock"
const URL = "http://localhost:8080/"
const SIGN_IN_PATH = "/fake/users/sign_in.json"
export const signInRequest = (status, payload = {}) => {
return nock(URL).get(SIGN_IN_PATH).reply(status, payload)
}
-
import { signInRequest } from "./../../utils/fakeRequests"
const doLogin = (browser) => {
return browser
.url("http://localhost:8080")
.waitForElementVisible('form', 1000)
.setValue('input[name=email]', 'foo@foo.com')
.setValue('input[name=password]', 'somepass')
.click('button[type=submit]')
.pause(500)
}
export default {
"Do login and shows error message": (browser) => {
signInRequest(403)
doLogin(browser)
.waitForElementVisible('.error', 1000)
.end()
}
}
Its possible mock http requests with nightwatch?
Nightwatch.js
is an end to end testing tool - so the point is that actual UIs as well as the api they call will be live (and not mocked)
So maybe you are looking for a framework designed for integration tests like casper.js
(http://casperjs.org/) or nightmare
(https://github.com/segmentio/nightmare)
However mocking http calls in nightwatch
should be doable with nock
i believe (if you have some strange use case that warrants it)
Ensure that you're nock http calls are before tests (they can even be in a before
block), eg:
module.exports = {
'test abc' : function (browser) {
nock('http://example.com')
.get('/users')
.query({name: 'martin'})
.reply(200, {results: [{id: '123'}]});
// do test stuff
},
};
Possible problem with your example is that your mocking the entire UI - nightwatch
might be somehow preventing the same domain from being intercepted. Having your UI and API on different domains (or ports) might solve the issue
来源:https://stackoverflow.com/questions/38404013/nightwatch-mock-http-requests