Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
W

wolfpack

> 编程语言
Open source

SailsJS Model Testing Library

99 stars0 likes0 views
WebsiteGitHub

About

SailsJS Model Testing Library

Wolfpack

Wolfpack helps you test your SailsJS models without the need for a db or lifting your sails app.

Basically, this is a library that instantiates your Sails models so that you can use them in your tests, without worrying about setting up a db, which introduces latency to your tests. By default, it spies all Model methods with sinonJS, so you can test if the correct parameters are being sent to the model on each call. It works like this:

…

The above example might look a little bit intimidating, especially if you are new to asynchronous testing. So in the next sections we are going to see how each component works separately. The previous example is also a sample of functional testing. With Wolfpack, you can do unit testing as well!

Table of Contents

  1. Installation
  2. Usage
  3. API
  4. wolpack
  5. wolfpack.setFindResults
  6. wolfpack.setCreateResults
  7. wolfpack.setUpdateResults
  8. wolfpack.clearResults
  9. wolfpack.setErrors
  10. wolfpack.clearErrors
  11. wolfpack.spy
  12. wolfpack.resetSpy
  13. wolfpack.resetSpies
  14. Conditional Mocking of the Results
  15. Model.setFindResults
  16. Model.setCreateResults
  17. Model.setUpdateResults
  18. Examples
  19. Mocking Model Results
  20. Mocking Errors
  21. Testing Sails Controllers
  22. Asynchronous Testing
  23. Testing Sails Models

Installation

To install wolfpack, simply do an npm install wolfpack --save-dev. This will add it to your package.json file to the devDependencies section. To use it in your applications, just require it in node as you usually do.

javascript
var wolfpack = require('wolfpack');

Usage

As stated in the pitch, Wolfpack lets you instantiate your Sails model so that you can test your model without having to connect to a database, by basically mocking and stubbing everything it can.

If you have Backbone testing backgrounds, this will be familiar to you. When testing a backbone model or collection, you instantiate it and provide mock data to test the methods. Rarely do you need your model or collection to communicate with the server to provide the results. That's because you want to test your model or collection, not how or if backbone is doing what it is supposed to do.

Instantiating a Sails model is simple. You just call the wolfpack() function and provide either the path to where the model is, or the model itself. In return it will provide you a Sails model.

javascript
var wolfpack = require('wolfpack');

var MyPathModel = wolfpack('path_to_app/api/models/MyModel');

var MyObjModel = wolfpack({
  attributes:{
    name: 'string',
    date: 'date'
  }
});
  

This will in return give you an instantiated model you can use to test the model itself, or controller behvaiour, with all the model methods originally provided by Sails, such as findOne, create, find, and dynamic finders. You can read more of the methods provided by Sails in their Models documentation.

The best part of this is that wolfpack, by default, spies on ALL methods, whether they were provided by Sails, you or your instance. The spies are provided by SinonJS, which therefore lets you know if a given method is called or not, with what arguments it was called, etc. To know what properties and methods are available for your spies, please read the SinonJS spies documentation.

API

wolfpack('path_to_model', [pk_type])

The wolfpack constructor allows you to instantiate a spied upon Sails model. You use it by calling wolfpack('path_to_model' || object) and pass as an argument either a string with the location of the model, or an object from which to build the model. All class and instance methods are spied on with SinonJS spies. Once instantiated, you can make your usual model calls.

javascript
var wolfpack = require('wolfpack');

var MyModel = wolfpack('path_to_app/api/models/MyModel');

MyModel.find({name: 'test'}).then(function(results){
  // ... more code ...
});

For testing controllers, you instantiate your model globally as Sails does on the background, like this:

javascript
var wolfpack = require('wolfpack');

global.MyModel = wolfpack('path_to_app/api/models/MyModel');

// You can now load your controller and it will have access to your wolfpack instantiated model
var MyController = require('path_to_app/api/controllers/MyController');

Additionally, to support testing of both NoSQL and SQL dbs, you can pass a second argument which determines the type of the id of the entry. For example, in mongo db, ids are strings, whereas in MySQL ids are integers.

When testing, these id differences may cause wolfpack to yield false results on find operations. By default, wolfpack sets up integer as the default type for id. However, if you are working in mongo, you can set it up to string.

For more information and examples on how to test, please go forward and read the examples sections, where we present several samples on how to use wolpack to test controllers, model classes, and instances of models.

wolfpack.setFindResults(object1 [,object2, object3, ...])

The wolfpack.setFindResults methods allows you to mock/fake data coming from the database. In other words, you can fake data coming from the database, and Sails will treat it as real data and build and instance from it (or not).

To fake the data, use wolfpack.setFindResults(results) where results is an object or an array of objects with the response "coming" from the database.

javascript
var wolfpack = require('wolfpack');

var MyModel = wolfpack('path_to_app/api/models/MyModel');

// Lets fake data from the db
wolfpack.setFindResults({name: 'John'});

MyModel.findOne(1).then(function(result){
  // result will be {name: 'John'}
});

As of version 0.3.0, you can now send a series of results to be mocked by wolfpack by passing each result as an individual argument. This means that serialized find calls will get different results according to what you set. Take a look at the following example:

javascript
var wolfpack = require('wolfpack');

var MyModel = wolfpack('path_to_app/api/models/MyModel');

// Lets fake data from the db
wolfpack.setFindResults({name: 'John'}, {name:'Doe'}, {name: Jane});

MyModel.findOne(1).then(function(result){
  // result will be {name: 'John'}
  MyModel.findOne(9).then(function(second_result){  // the 9 is just a random number
    // second_result will be {name: 'Doe'}
    MyModel.findOne(1).then(function(third_result){ // no matter what we send, it will pull from the results given
      // third_result will be {name: 'Jane'}
    });
  });
});

When Wolfpack runs out of results (in the example above after the third call), it will simply return the last given result always, meaning {name: 'Jane'} will be the result it will give from now on, once the pool of results have been used.

It is also important to note that the serialized results will apply to any model, so you can test actions in multiple model this way:

javascript
var wolfpack = require('wolfpack');

var MyModel = wolfpack('path_to_app/api/models/MyModel');
var MyOtherModel = wolfpack('path_to_app/api/models/MyOtherModel');

// Lets fake data from the db
wolfpack.setFindResults({id: 1, name: 'John'}, {ownerId: 1, cash: 300});

MyModel.findOne(1).then(function(result){
  // result will be {id: 1, name: 'John'}
  MyOtherModel.findOne(9).then(function(second_result){  // the 9 is just a random number
    // second_result will be {ownerId: 1, cash: 300}
  });
});

Please note, if you set any results with the wolfpack.setFindResults method, all future find calls to any model will return those results, with some exceptions explained later. If you call it to set other results, then those results will always be returned, and so on. To stop sending those fake results, use the wolfpack.clearResults method.

wolfpack.setCreateResults(object1 [, object2, object3, ...])

Just as wolfpack.setFindResults, the wolfpack.createResults method will allow you to set the fake db response for any create operation. You can pass one object, or a series of objects as individual arguments to set the results that will be return on a create operation.

javascript
var wolfpack = require('wolfpack');

var MyModel = wolfpack('path_to_app/api/models/MyModel');

// Lets fake data from the db
wolfpack.setCreateResults({name: 'John'});

MyModel.create({name: 'Doe'}).then(function(result){
  // Result will be {name: 'John', updatedAt: someDate, createdAt: someDate}
  // Notice that even though we sent 'Doe' as name, the setCreateResults returned 'John' as we requested
});

Again, as with setFindResults, all future create events will have this response, until changed with another setCreateResults or until the clearResults method is called.

Also, as with setFindResults, you can pass a series of results to be return in a series of create operations (useful when you have operations on separate models):

…

wolfpack.setUpdateResults(object1 [, object2, object3, ...])

The wolfpack.setUpdateResults method allows you to set the fake db results for all update operations. You pass an object or array of objects as individual arguments for the results you want to fake. Pass several arguments to mock the results in the order of the arguments.

It is important that, when you are faking update actions, you set an id for the instantiated model. Otherwise, the update action will throw an error.

javascript
var wolfpack = require('wolfpack');

var MyModel = wolfpack('path_to_app/api/models/MyModel');

// First we need a model to update, so lets fake it
wolfpack.setFindResults({id: 1, name: 'John'}); // Notice the id = 5) {
        return callback('Room full');
      }

      // Add user to chatroom
      room.addUser(username, callback);
    }).catch(function(err){
      return callback(err.message || err);
    });
  }
};

Above is our model definition that will be instantiated by wolfpack. Seems a little messy, especially because of all methods called addUser, but it serves its point. Let's move on to testing.

There's a couple of stuff we need to test in our model. We need to test that the class method is doing what it is supposed to do, and we need to test the instance method is doing what it is supposed to. Lets write first the test for the class.

…

Really long test that one, especially the last part. However, as you may soon be noticing, we are achieving 100% te

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

JavaScript

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 18, 2026
Category编程语言
PricingOpen source

> Related tools

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言