unit testing环回模型

使用Loopback,我们创build了一些自定义的远程方法,并且我们要unit testing这个逻辑。 我期望完成的是加载一个模型,而不是所有的模型,并且unit testing那个模型的自定义远程方法。

我们可以将这个模型连接到内存数据库(而不是我们的例子中的Postgres),但是不知何故,我需要告诉Loopback这个独立的模型,而不使用Loopback引导。 (如果我们使用标准的Loopback引导(app.boot()),它会加载我们所有的模型和整个shebang,我认为我们应该避免孤立的目的)。

我们在一个正在进行的unit testing中有这个设置:

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(); } }); }); 

所以不是使用Loopback引导,而是要加载模型模式和模型逻辑,然后以独立方式将其附加到Loopback应用程序。

有没有我们可以使用的Loopback调用,将此模型附加到Loopback服务器/应用程序?

我正在寻找这种types的电话:

 app.useModel(Contact); 

基本上我想要做的是这样的:

 app.models.Contact = Contact; 

但这绝对是错误的做法 – 只是寻找正确的API调用。

也许这是正确的电话?

 Contact.attachTo(loopback.memory()); 

免责声明:我是LoopBack的维护者,也是loopback-boot @ 2的原作者

build立一个模型的规范方法(也被引擎盖下的loopback-boot使用)是调用app.registry.createModel(json) ,然后app.model(ModelCtor, config)

在你的具体情况下:

 const app = loopback(); // Consider using local per-app registry of models to avoid // interference between tests. By default, all LoopBack apps // share the same global registry (one per process) // const app = loopback({ localRegistry: true }); // create in-memory datasources app.dataSource('db', { connector: 'memory' }); //load the schema for the model const ContactSchema = require(path.resolve(projectRoot + '/server/models/contact.json')); const Contact = app.registry.createModel(ContactSchema); //load remote methods for this model require(path.resolve(projectRoot + '/server/models/contact.js'))(Contact); // Caveat lector: the configuration may contain more than just dataSource, // It may be safer to read the model configuration from "server/model-config" // and override "dataSource" property. app.model(Contact, { dataSource: 'db' }); // setup REST API app.use('/api', loopback.rest()); // now we are good to start testing const supertest = require('supertest'); 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(); } }); }); 

我用这种方法看到两个可能的警告:

  • 如果您的自定义远程方法正在访问其他/相关模型,则在此设置中会失败,因为这些模型不可用。
  • 您的服务器没有在server/middleware.json middleware.json中configuration任何中间件,也没有从引导脚本中添加任何其他server/middleware.json

我个人build议你尝试使用loopback-boot,但是覆盖dataSources和要在应用程序中configuration的模型列表。 大致如下:

 const app = loopback(); boot(app, { appRootDir: path.resolve('../server'), env: 'unit-test', // Alternatively, the "dataSources" configuration for tests // can be provided in "server/datasources.unit-test.json" dataSources: { db: { connector: 'memory' } }, models: { Contact: { // as I mentioned before, it's probably better to load this section // from "server/model-config.json" dataSource: 'db' } }, }); 

这是有效的,因为loopback-boot会延迟加载模型,也就是只有在应用程序及其父母中configuration的模型。