Skip to content

Instantly share code, notes, and snippets.

@pulkitsinghal
Created July 8, 2016 18:35
Show Gist options
  • Save pulkitsinghal/d48a25bdc0dfbbf80adc87c0cf8cf10b to your computer and use it in GitHub Desktop.
Save pulkitsinghal/d48a25bdc0dfbbf80adc87c0cf8cf10b to your computer and use it in GitHub Desktop.
Use `loopback-testing` module with mongodb
// path: <project>/test/KPIndex.test.js
require('debug').enable('loopback:connector:*'); //enable namespace
var debug = require('debug')('loopback:connector:*'); //reload debug
var lt = require('loopback-testing');
var assert = require('assert');
var app = require('../server/server.js'); //path to app.js or server.js
var KpIndex;
var TestDataBuilder = require('loopback-testing').TestDataBuilder;
var ref = TestDataBuilder.ref;
// The context object to hold the created models.
// You can use `this` in mocha test instead.
var context = {};
var ref = TestDataBuilder.ref;
// Connect to db
//console.log(app);
//console.log(app.datasources.mongodb);
var dbConnector = app.datasources.mongodb;
/**
* Tests for KPIndex route
*/
describe('/KpIndex', function() {
this.timeout(5000);
lt.beforeEach.withApp(app);
/**
* Wait for app to get started
* @Todo modify to real async processing instead of only wait and hope.
*/
beforeEach(function(done){
//setTimeout(done,1000);
// Create a new model and attach the mixin
var KpIndices = this.KpIndices = app.PersistedModel.extend(
'kp-indice',
{name: String},
{}
);
// Attach model to db
KpIndices.attachTo(dbConnector);
app.model(KpIndices);
app.use(loopback.rest());
app.set('legacyExplorer', false);
new lt.TestDataBuilder()
.define('kp-indice', KpIndices, {
name: 'Item name'
})
.buildTo(this, done);
});
lt.describe.whenCalledRemotely('GET', '/api/KpIndices', function () {
lt.it.shouldBeAllowed();
it('should have statusCode 200', function() {
assert.equal(this.res.statusCode, 200);
});
lt.beforeEach.givenModel('KpIndex',{
date: new Date()
});
it('should respond with an array of KpIndices', function () {
assert(Array.isArray(this.res.body));
});
});
lt.describe.whenCalledRemotely('GET', '/api/KpIndices/count', function () {
lt.it.shouldBeAllowed();
it('should have statusCode 200', function() {
assert.equal(this.res.statusCode, 200);
});
it('should respond with an number ', function () {
assert.notEqual(this.res.body.count, undefined);
assert.ok(this.res.body.count > 0);
});
});
lt.describe.whenCalledRemotely('GET', '/api/KpIndices/current', function () {
lt.it.shouldBeAllowed();
it('should have statusCode 200', function() {
assert.equal(this.res.statusCode, 200);
});
it('should respond with one KPIndex entry ', function () {
assert(this.res.body instanceof Object);
assert(this.res.body.hasOwnProperty("date"));
assert(this.res.body.hasOwnProperty("utc"));
assert(this.res.body.hasOwnProperty("kpValue"));
});
});
lt.describe.whenCalledRemotely('GET', '/api/KpIndices/prediction', function () {
lt.it.shouldBeAllowed();
it('should have statusCode 200', function() {
assert.equal(this.res.statusCode, 200);
});
lt.beforeEach.givenModel('KpIndex',{
date: new Date()
});
it('should respond with an array of KpIndices', function () {
assert(Array.isArray(this.res.body));
});
it('should return more then one item', function () {
assert.ok(this.res.body.length > 1)
});
//assert.equal(results.length, 0);
});
});
lt.describe.whenCalledRemotely('GET', '/api/KpIndices/prediction/daily', function () {
lt.it.shouldBeAllowed();
it('should have statusCode 200', function() {
assert.equal(this.res.statusCode, 200);
});
lt.beforeEach.givenModel('KpIndex',{
date: new Date()
});
it('should respond with an array of KpIndices', function () {
assert(Array.isArray(this.res.body));
});
it('should return exact 3 items', function () {
assert.ok(this.res.body.length === 3 )
});
});
@pulkitsinghal
Copy link
Author

refer to https://github.com/ShoppinPal/loopback-mongo-sandbox/tree/master/test for latest stable attempts at making loopback-testing work with mongodb

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment