Using Loopback, we have created some custom remote methods, and we want to unit test that logic. What I am looking to accomplish is to load just one model, not all our models, and unit test the custom remote method(s) for that one model.
We can connect this model to the memory database (instead of Postgres in our case), but somehow I need to tell Loopback about this isolated model, without using Loopback boot. (If we use the standard Loopback boot (app.boot()), it will load all our models and the whole shebang, which I think we should avoid for isolation purposes).
We have this setup in a unit test that is a work in progress:
const supertest = require('supertest');
//load the schema for the model
const ContactSchema = require(path.resolve(projectRoot + '/server/models/contact.json'));
const opts = {
strict: true
};
const dataSource = loopback.createDataSource({
connector: loopback.Memory
});
const Contact = dataSource.createModel('Contact', ContactSchema, opts);
//load remote methods for this model
require(path.resolve(projectRoot + '/server/models/contact.js'))(Contact);
const app = loopback();
this.it.cb('test contact', t => {
supertest(app).get('/api/Contacts')
.expect(200)
.end(function (err, res) {
if (err) {
t.fail(err); // we naturally get a 404, because the model hasn't been attached to this Loopback server
}
else {
t.done();
}
});
});
so instead of using Loopback boot, I want to load the model schema and model logic and then attach it to the Loopback app in an isolated way.
Is there a Loopback call we can use, to attach this model to the Loopback server/app?
I am looking for this type of call:
app.useModel(Contact);
Essentially what I am looking to do is something like this:
app.models.Contact = Contact;
but that is definitely the wrong way to do it - just looking for the right API call.
Perhaps this is the right call?
Contact.attachTo(loopback.memory());