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

Commit 42ce905

Browse files
author
Ryan Schumacher
committed
Add ability to run whenCalledRemotely only once
1 parent afff664 commit 42ce905

File tree

3 files changed

+70
-0
lines changed

3 files changed

+70
-0
lines changed

README.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,36 @@ describe('/products', function() {
6969
});
7070
```
7171

72+
### call remote only once
73+
74+
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) {..})`)
75+
76+
77+
```js
78+
var lt = require('loopback-testing');
79+
var assert = require('assert');
80+
var app = require('../server/server.js'); //path to app.js or server.js
81+
82+
describe('/products', function() {
83+
lt.beforeEach.withApp(app);
84+
lt.describe.whenCalledRemotely('POST', '/products', {
85+
name: 'product-1'
86+
}, function($once) {
87+
88+
var id;
89+
90+
it('should have statusCode 200', function() {
91+
id = this.res.body.id;
92+
assert.equal(this.res.statusCode, 200);
93+
});
94+
95+
it('should have only been called once', function() {
96+
assert.equal(this.res.body.id, id);
97+
});
98+
});
99+
});
100+
```
101+
72102
## building test data
73103

74104
Use TestDataBuilder to build many Model instances in one async call. Specify

lib/helpers.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,8 +240,18 @@ _describe.whenCalledRemotely = function(verb, url, data, cb) {
240240
urlStr = '/<dynamic>';
241241
}
242242

243+
var once = cb.toString().match(/^function \([^\)]*\$once[^\)]*\)/) ? false : null;
244+
243245
describe(verb.toUpperCase() + ' ' + urlStr, function() {
244246
beforeEach(function(cb) {
247+
if(once !== null) {
248+
if(once === true) {
249+
cb();
250+
return;
251+
}
252+
once = true;
253+
}
254+
245255
if(typeof url === 'function') {
246256
this.url = url.call(this);
247257
}

test/test.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,18 @@ describe('helpers', function () {
6767
helpers.describe.staticMethod('create', function() {
6868
helpers.beforeEach.withArgs({foo: 'bar'});
6969
helpers.describe.whenCalledRemotely('POST', '/xxx-test-models', function() {
70+
71+
var id;
72+
7073
it('should call the method over rest', function () {
74+
id = this.res.body.id;
7175
assert.equal(this.res.statusCode, 200);
7276
});
77+
78+
it('should be called again have new id', function () {
79+
assert.notEqual(this.res.body.id, id);
80+
});
81+
7382
});
7483
});
7584
helpers.describe.staticMethod('findById', function() {
@@ -109,4 +118,25 @@ describe('helpers', function () {
109118
});
110119
});
111120
});
121+
122+
describe('whenCalledRemotely once', function() {
123+
helpers.describe.staticMethod('create', function() {
124+
helpers.beforeEach.withArgs({foo: 'bar'});
125+
helpers.describe.whenCalledRemotely('POST', '/xxx-test-models', function($once) {
126+
127+
var id;
128+
129+
it('should call the method over rest', function () {
130+
id = this.res.body.id;
131+
assert.equal(this.res.statusCode, 200);
132+
});
133+
134+
it('should have only been called once', function () {
135+
assert.equal(this.res.body.id, id);
136+
});
137+
138+
});
139+
});
140+
});
141+
112142
});

0 commit comments

Comments
 (0)