Skip to content
This repository was archived by the owner on May 29, 2019. It is now read-only.

Add ability to run whenCalledRemotely only once #43

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -69,6 +69,36 @@ describe('/products', function() {
});
```

### call remote only once

By default `whenCalledRemotely` will be run before each `it()`. This can be changed to run only once by adding a `$once` as a param to the callback function (i.e. `lt.describe.whenCalledRemotely('GET', '/', function($once) {..})`)


```js
var lt = require('loopback-testing');
var assert = require('assert');
var app = require('../server/server.js'); //path to app.js or server.js

describe('/products', function() {
lt.beforeEach.withApp(app);
lt.describe.whenCalledRemotely('POST', '/products', {
name: 'product-1'
}, function($once) {

var id;

it('should have statusCode 200', function() {
id = this.res.body.id;
assert.equal(this.res.statusCode, 200);
});

it('should have only been called once', function() {
assert.equal(this.res.body.id, id);
});
});
});
```

## building test data

Use TestDataBuilder to build many Model instances in one async call. Specify
10 changes: 10 additions & 0 deletions lib/helpers.js
Original file line number Diff line number Diff line change
@@ -240,8 +240,18 @@ _describe.whenCalledRemotely = function(verb, url, data, cb) {
urlStr = '/<dynamic>';
}

var once = cb.toString().match(/^function \([^\)]*\$once[^\)]*\)/) ? false : null;

describe(verb.toUpperCase() + ' ' + urlStr, function() {
beforeEach(function(cb) {
if(once !== null) {
if(once === true) {
cb();
return;
}
once = true;
}

if(typeof url === 'function') {
this.url = url.call(this);
}
30 changes: 30 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -67,9 +67,18 @@ describe('helpers', function () {
helpers.describe.staticMethod('create', function() {
helpers.beforeEach.withArgs({foo: 'bar'});
helpers.describe.whenCalledRemotely('POST', '/xxx-test-models', function() {

var id;

it('should call the method over rest', function () {
id = this.res.body.id;
assert.equal(this.res.statusCode, 200);
});

it('should be called again have new id', function () {
assert.notEqual(this.res.body.id, id);
});

});
});
helpers.describe.staticMethod('findById', function() {
@@ -109,4 +118,25 @@ describe('helpers', function () {
});
});
});

describe('whenCalledRemotely once', function() {
helpers.describe.staticMethod('create', function() {
helpers.beforeEach.withArgs({foo: 'bar'});
helpers.describe.whenCalledRemotely('POST', '/xxx-test-models', function($once) {

var id;

it('should call the method over rest', function () {
id = this.res.body.id;
assert.equal(this.res.statusCode, 200);
});

it('should have only been called once', function () {
assert.equal(this.res.body.id, id);
});

});
});
});

});