Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unit testing for loopback model

I have a Loopback API with a model Student.

How do I write unit tests for the node API methods of the Student 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?

like image 258
Ajay Kumar Avatar asked Sep 27 '16 07:09

Ajay Kumar


People also ask

How do you test a LoopBack?

To perform an external loopback test on an Ethernet interface, connect a loopback plug to the Ethernet interface. The device sends test packets out of the interface, which are expected to loop over the plug and back to the interface.

What is the purpose of unit testing?

The main objective of unit testing is to isolate written code to test and determine if it works as intended. Unit testing is an important step in the development process, because if done correctly, it can help detect early flaws in code which may be more difficult to find in later testing stages.

What is Unit Testing in C# with example?

A Unit Test is a code written by any programmer which test small pieces of functionality of big programs. Performing unit tests is always designed to be simple, A "UNIT" in this sense is the smallest component of the large code part that makes sense to test, mainly a method out of many methods of some class.


1 Answers

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 :)

like image 61
Overdrivr Avatar answered Sep 28 '22 11:09

Overdrivr