Ajay Kumar Ajay Kumar - 1 year ago 199
Node.js Question

Unit testing for loopback model

I have a Loopback API with a model


How do I write unit tests for the node API methods of the
model without calling the REST API? I can't find any documentation or examples for testing the model through node API itself.

Can anyone please help?

Answer Source

Example with testing the count method

// With this test file located in ./test/thistest.js

var app = require('../server');

describe('Student node api', function(){
  it('counts initially 0 student', function(cb){
      app.models.Student.count({}, function(err, count){
        assert.deepEqual(count, 0);

This way you can test the node API, without calling the REST API.

However, for built-in methods, this stuff is already tested by strongloop so should pretty useless to test the node API. But for remote (=custom) methods it can still be interesting.

EDIT: The reason why this way of doing things is not explicited is because ultimately, you will need to test your complete REST API to ensure that not only the node API works as expected, but also that ACLs are properly configured, return codes, etc. So in the end, you end up writing 2 different tests for the same thing, which is a waste of time. (Unless you like to write tests :)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download