Skip to content

Repository files navigation

Unofficial loopback-testing

Utilities for testing LoopBack apps

Build Status

Unofficial

Strongloop has deprecated the original loopback-testing. This repository is a fork made by iGLOO. Feel free to contribute!

overview

The following helpers are designed to generate [mocha] tests against LoopBack apps.

install

  1. npm install github:igloo-be/loopback-testing --save-dev
  2. Assuming you started with a clean template/project generated by slc loopback
  3. If you have mocha installed as a global npm module that's great! Simply update <your_project>/package.json with:
```
{
...
"scripts": {
...
"test": "mocha"
}
}
```
  1. Otherwise, you can utilize the mocha library within the loopback-testing testing module:
```
{
...
"scripts": {
...
"test": "./node_modules/loopback-testing/node_modules/.bin/mocha"
}
}
```
  1. Run npm test to execute any tests under the test directory.

basic usage

Below is a simple LoopBack app.

varloopback=require('loopback');varapp=loopback();varProduct=app.model('product');Product.attachTo(loopback.memory());

Use the loopback-testing module to generate mocha tests.

varlt=require('loopback-testing');varassert=require('assert');varapp=require('../server/server.js');//path to app.js or server.jsdescribe('/products',function(){lt.beforeEach.withApp(app);lt.describe.whenCalledRemotely('GET','/products',function(){lt.it.shouldBeAllowed();it('should have statusCode 200',function(){assert.equal(this.res.statusCode,200);});lt.beforeEach.givenModel('product');it('should respond with an array of products',function(){assert(Array.isArray(this.res.body));});});});

building test data

Use TestDataBuilder to build many Model instances in one async call. Specify only properties relevant to your test, the builder will pre-fill remaining required properties with sensible defaults.

varTestDataBuilder=require('loopback-testing').TestDataBuilder;varref=TestDataBuilder.ref;// The context object to hold the created models.// You can use `this` in mocha test instead.varcontext={};varref=TestDataBuilder.ref;newTestDataBuilder().define('application',Application,{pushSettings: {stub: {}}}).define('device',Device,{// use the value of application's id// the value is resolved at build timeappId: ref('application.id'),deviceType: 'android'}).define('notification',Notification).buildTo(context,function(err){// test models are available as// context.application// context.device// context.notification});

Function reference

LoopBack Testing (lt):

  • describe
  • it
  • beforeEach
  • TestDataBuilder

lt.describe:

  • staticMethod
  • instanceMethod
  • whenCalledRemotely
  • whenLoggedInAsUser
  • whenLoggedInAsUserWithRole
  • whenCalledByUser
  • whenCalledByUserWithRole
  • whenCalledAnonymously
  • whenCalledUnauthenticated

lt.it:

  • shouldBeAllowed
  • shouldBeDenied
  • shouldNotBeFound
  • shouldBeForbidden
  • shouldBeRejected
  • shouldBeAllowedWhenCalledAnonymously
  • shouldBeDeniedWhenCalledAnonymously
  • shouldBeAllowedWhenCalledUnauthenticated
  • shouldBeDeniedWhenCalledUnauthenticated
  • shouldBeAllowedWhenCalledByUser
  • shouldBeDeniedWhenCalledByUser
  • shouldBeAllowedWhenCalledByUserWithRole
  • shouldBeDeniedWhenCalledByUserWithRole

lt.beforeEach:

  • withApp
  • withArgs
  • givenModel
  • givenUser
  • givenUserWithRole
  • givenLoggedInUser
  • givenLoggedInUserWithRole
  • givenAnUnauthenticatedToken
  • givenAnAnonymousToken

About

Utilities for testing LoopBack apps

Resources

Contributing

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages