![Oracle Drags Its Feet in the JavaScript Trademark Dispute](https://cdn.sanity.io/images/cgdhsj6q/production/919c3b22c24f93884c548d60cbb338e819ff2435-1024x1024.webp?w=400&fit=max&auto=format)
Security News
Oracle Drags Its Feet in the JavaScript Trademark Dispute
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
waterline-fakes
Advanced tools
Utilities to make it easier to fake out waterline models and collections for unit testing
Utilities to make it easier to fake out waterline models and collections for unit testing.
When unit testing controllers or services in Sails.js, it can be helpful to replace model and collection interaction results with fake results, so that you're only testing the one layer of your code that you are focusing on. This package exposes some factories to generate objects and methods that would take the place of normal Sails model or collection methods.
npm i --save-dev waterline-fakes
var waterlineFakes = require('waterline-fakes');
/* => {
fakeWaterlineChainMethod: function () {},
fakeWaterlineModel: function () {}
} */
Used to replace a method on a Collection, that would normally be followed by a call to
.exec(function (err, results)) {}
. A normal block of code that you would want to test may look something like this:
// GET /pets
list: function (req, res) {
Pets.find(req.query).exec(function (err, pets) {
if (err) {
res.send(500, { error: 'internal server error' });
return;
}
res.send(200, pets);
});
}
To test the error condition of this controller action using mocha
, you could do something like this:
var req = require('supertest');
var sinon = require('sinon');
var expect = require('chai').expect;
var fakeWaterlineChainMethod = require('waterline-fakes').fakeWaterlineChainMethod;
describe('the list method', function () {
describe('when an error occurs looking up the pets', function () {
beforeEach(function () {
sinon.stub(Pets, 'find', fakeWaterlineChainMethod({
err: new Error('boom');
}));
});
it('returns 500 status code and error json', function (done) {
req(sails.hooks.http.server)
.get('/pets')
.expect(500)
.expect('Content-Type', /json/)
.end(function (err, res) {
expect(err).to.not.exist();
expect(res.body).to.deep.equal({
error: 'internal server error'
});
done();
});
});
});
});
var fakeWaterlineChainMethod = require('waterline-fakes').fakeWaterlineChainMethod;
var fakeMethod = fakeWaterlineChainMethod({
err: {}, // Default: null. The error value you want the exec callback to be called with
result: {} // Default: []. The result value you want the exec callback to be called with
});
// if you pass both err and result, the err will take precedence. so... don't do that.
Used to simulate an actual Waterline model, if your controller or service code interacts with the model directly. This
is usually passed as the result
option to fakeWaterlineChainMethod when needed.
Take this other example controller action:
// PUT /pets/:id
update: function (req, res) {
Pets.findOneById(req.params.id).exec(function (err, pet) {
if (err) {
res.send(500, { error: 'internal server error' });
return;
}
if (!pet) {
res.send(404, { error: 'not found' });
return;
}
pet.name = req.body.name;
pet.color = req.body.color;
pet.type = req.body.type;
pet.save(function (err, updatedPet) {
if (err) {
res.send(500, { error: 'internal server error' });
return;
}
res.send(200, updatedPet);
};
});
}
You could test the error condition of the pet.save()
using the following mocha
test:
var req = require('supertest');
var sinon = require('sinon');
var expect = require('chai').expect;
var waterlineFakes = require('waterline-fakes');
var fakeWaterlineChainMethod = waterlineFakes.fakeWaterlineChainMethod;
var fakeWaterlineModel = waterlineFakes.fakeWaterlineModel;
describe('the update method', function () {
describe('when an error occurs while saving the updated pet', function () {
beforeEach(function () {
var fakePet = fakeWaterlineModel({
save: { err: new Error('boom') }
});
sinon.stub(Pets, 'findOneById', fakeWaterlineChainMethod({ result: fakePet }));
});
it('returns 500 status code and error json', function (done) {
req(sails.hooks.http.server)
.put('/pets/foo')
.send({
name: 'Lenny',
type: 'golden retriever',
color: 'champagne'
}).expect(500)
.expect('Content-Type', /json/)
.end(function (err, res) {
expect(err).to.not.exist();
expect(res.body).to.deep.equal({
error: 'internal server error'
});
done();
});
});
});
});
var fakeWaterlineModel = require('waterline-fakes').fakeWaterlineModel;
var fakeModel = fakeWaterlineModel({
props: {}, // properties you want the model to have. would be available at, for example, model.foo
destroy: {
err: {} // Default: null. The error value you want the destroy callback to be called with
},
save: {
err: {}, // Default: null. The error value you want the save callback to be called with
result: {} // Default: model. The result value you want the save callback to be called with.
}
});
// if you pass both err and result, the err will take precedence. so... don't do that.
To contribute, see CONTRIBUTING.md
1.0.1 - 2015-05-14
FAQs
Utilities to make it easier to fake out waterline models and collections for unit testing
The npm package waterline-fakes receives a total of 5 weekly downloads. As such, waterline-fakes popularity was classified as not popular.
We found that waterline-fakes demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 6 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
Security News
The Linux Foundation is warning open source developers that compliance with global sanctions is mandatory, highlighting legal risks and restrictions on contributions.
Security News
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.