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

Commit

Permalink
Add ability to run whenCalledRemotely only once
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryan Schumacher committed Feb 4, 2015
1 parent afff664 commit 42ce905
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 0 deletions.
30 changes: 30 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions lib/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
30 changes: 30 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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);
});

});
});
});

});

0 comments on commit 42ce905

Please sign in to comment.