describe('/Student', function () {
var server = require('../server/server')
var loopback = require('loopback')
var supertest = require('supertest')
var request = require('supertest')(server)
var Student = dataSource.define('Student', {
'id': Number,
'points': Number
});
beforeEach(function () {
Student.updateOrCreate({id: 1, points: 5000});
})
it('Post a new student', function (done) {
request.post('/api/Students').send({points: 5000}).expect(200, done)
})
})
I create a model instead of using server.model.Student. This apparently enables the models that are created to bypass the model creating code before save and after save.
This obviously has pros and cons. The pro being that the initialization code (Student.updateOrCreate) is not limited to the validation code that I have in the model. Which is obviously also the con. With the code below, I am able to run all the tests from multiple different models as a single suite using npm test without any failures.
In the case of Student, there is code which prevents the id from being explicitly set (so it nice to simply bypass that code using create Model.) I don't know if this was the intention, but it is a nice side-effect to simplify initializing the instances.
describe('/Student', function () {
var server = require('../server/server')var request = require('supertest')(server)
var dataSource = server.dataSource('db', {adapter: 'memory'})
var Student
before(function() {
Student = dataSource.createModel('Student', apps.models.Student)
}
beforeEach(function () {
Student.updateOrCreate({id: 1, points: 5000});
})
it('Post a new student', function (done) {
request.post('/api/Students').send({points: 5000}).expect(200, done)
})
})