waterline-fakes
Utilities to make it easier to fake out waterline models and collections for unit testing.
description
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.
install
npm i --save-dev waterline-fakes
api
var waterlineFakes = ;/* => { fakeWaterlineChainMethod: function () {}, fakeWaterlineModel: function () {}} */
using fakeWaterlineChainMethod(options)
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 { Pets;}
To test the error condition of this controller action using mocha
, you could do something like this:
var req = ;var sinon = ;var expect = expect;var fakeWaterlineChainMethod = fakeWaterlineChainMethod; ;
fakeWaterlineChainMethod api
var fakeWaterlineChainMethod = fakeWaterlineChainMethod;var fakeMethod = ;// if you pass both err and result, the err will take precedence. so... don't do that.
using fakeWaterlineModel(options)
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 { Pets
You could test the error condition of the pet.save()
using the following mocha
test:
var req = ;var sinon = ;var expect = expect;var waterlineFakes = ;var fakeWaterlineChainMethod = waterlineFakesfakeWaterlineChainMethod;var fakeWaterlineModel = waterlineFakesfakeWaterlineModel; ;
fakeWaterlineModel api
var fakeWaterlineModel = fakeWaterlineModel;var fakeModel = ;// if you pass both err and result, the err will take precedence. so... don't do that.
contributing
To contribute, see CONTRIBUTING.md