Loopback模型的单元测试

5
使用Loopback,我们创建了一些自定义远程方法,并希望对该逻辑进行单元测试。我想要实现的是仅加载一个模型,而不是所有模型,并对该模型的自定义远程方法进行单元测试。
我们可以将此模型连接到内存数据库(而不是在我们的情况下使用Postgres),但不知何故,我需要告诉Loopback关于这个隔离的模型,而不使用Loopback boot。(如果我们使用标准的Loopback boot(app.boot()),它将加载所有模型和整个过程,我认为我们应该避免这种情况以达到隔离的目的)。
我们在进行中的单元测试中设置了这个设置:
  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 boot,而是想要加载模型模式和模型逻辑,并将其以隔离的方式附加到Loopback应用程序中。

是否有Loopback调用可用于将此模型附加到Loopback服务器/应用程序中?

我正在寻找这种类型的调用:

app.useModel(Contact);

基本上我想做的是像这样的事情:
app.models.Contact = Contact;

但这绝对不是正确的做法 - 只需要找到正确的 API 调用。

也许这就是正确的调用方式?

Contact.attachTo(loopback.memory());

1
“隔离目的”是什么意思?仅使用回环引导并仅测试特定模型没有任何问题。 - Overdrivr
严格意义上的单元测试可能意味着您应该尽可能地隔离事物,甚至不使用实时服务器;但是,我很难在不启动Loopback Boot服务器的情况下测试模型代码。 - Alexander Mills
再说,我个人认为单元测试是浪费时间。 - Alexander Mills
2
我同意你的看法,但是当我们测试LoopBack API时,我们正在尝试实现更多的集成测试。在这种情况下,最好使用loopback-boot启动服务器,并尽可能接近生产环境。 - Overdrivr
1
基本上100%同意。 - Alexander Mills
1个回答

5

免责声明:本人是LoopBack维护者和loopback-boot@2的原作者

设置模型的规范方式(也是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中配置任何中间件,也没有从引导脚本中添加任何其他内容。

我个人建议您尝试使用loopback-boot,但覆盖dataSources和要在应用程序中配置的模型列表。可以使用以下方式:

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懒加载模型,即仅加载已在应用程序中配置的模型及其父级。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接