Skip to content

Instantly share code, notes, and snippets.

@tobythetester
Created May 1, 2016 12:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tobythetester/c375750ff921220e9d99f93f49e712b6 to your computer and use it in GitHub Desktop.
Save tobythetester/c375750ff921220e9d99f93f49e712b6 to your computer and use it in GitHub Desktop.
Mocked response from file
var nock = require('nock');
var request = require('supertest')("http://api.postcodes.io");
var expect = require('chai').expect;
//used to read the JSON file
var fs = require("fs");
describe("Testing API with a mocked backend", function () {
it("responds with json file response", function (done) {
//read the json file
var contents = fs.readFileSync('mockedresponse.json');
//parse the contents and assign to a variable
var jsonContent = JSON.parse(contents);
nock("http://api.postcodes.io")
.get('/postcodes/')
.reply(200, jsonContent);
request
.get('/postcodes/')
.expect(200)
.end(function (err, res) {
expect(res.body.status).to.equal(200);
expect(res.body.result.postcode).to.equal("SW1A 1AA")
done();
});
})
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment