gpt4 book ai didi

node.js - 单元测试环回模型

转载 作者:搜寻专家 更新时间:2023-10-31 23:41:07 37 4
gpt4 key购买 nike

使用 Loopback,我们创建了一些自定义的远程方法,我们想要对该逻辑进行单元测试。我想要完成的是只加载一个模型,而不是我们所有的模型,并对那个模型的自定义远程方法进行单元测试。

我们可以将这个模型连接到内存数据库(在我们的例子中不是 Postgres),但是我需要以某种方式告诉 Loopback 这个孤立的模型,而不使用 Loopback 引导。 (如果我们使用标准的 Loopback 引导 (app.boot()),它将加载我们所有的模型和整个 shebang,我认为出于隔离目的我们应该避免这种情况。

我们在正在进行的单元测试中进行了此设置:

  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 应用。

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

我正在寻找这种类型的电话:

app.useModel(Contact);

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

app.models.Contact = Contact;

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

也许这是正确的决定?

Contact.attachTo(loopback.memory());

最佳答案

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

设置模型的规范方法(也被 loopback-boot 使用)是调用 app.registry.createModel(json) 然后 app.model (ModelCtor, 配置).

在您的特定情况下:

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,但要覆盖数据源和要在应用程序中配置的模型列表。大致如下:

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

关于node.js - 单元测试环回模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40229171/

37 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com