簡體   English   中英

單元測試環回模型

[英]Unit testing Loopback models

使用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啟動。

我們可以使用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, 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中配置任何中間件,也沒有從引導腳本中添加任何其他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會懶惰加載模型,即只在應用程序中配置的模型及其父項。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM