简体   繁体   English

如何实例化要在单元测试中使用的Sequelize模型?

[英]How can I instantiate a Sequelize model to be used in Unit Tests?

I am trying to write unit tests for a Sequelize model. 我正在尝试为Sequelize模型编写单元测试。 I have an instance method on the class that call's Sequelize's update method. 我在调用Sequelize的update方法的类上有一个实例方法。 Turning update into a mocked function is no problem. update变成模拟功能没问题。 I just can't seem to figure out how to properly do a X = new MyModel() without some kind of error. 我只是似乎无法弄清楚如何正确地执行X = new MyModel()而没有某种错误。 It tells me Class constructor model cannot be invoked without 'new' . 它告诉我, Class constructor model cannot be invoked without 'new'

I took a look at https://sequelize-mock.readthedocs.io/en/stable/ but the way it's written makes me think it's better suited for mocking the Models when testing classes that are taking advantage of a model, rather than the model itself. 我看了https://sequelize-mock.readthedocs.io/en/stable/,但是它的编写方式使我认为它更适合在测试利用模型的类时模拟模型,而不是模型。模型本身。

const BaseUtut = sequelizeInstance.define('utut', {
  id: {
    type: Sequelize.INTEGER,
    primaryKey: true,
    autoIncrement: true
  },
  metadata: {
    type: Sequelize.JSON
  }
});

class Utut extends BaseUtut {
  async updateFromFlat(id, metadata) {

    // in reality, i modify metadata, and want to test things around that
    modifiedMetadata = methodThatModified(metadata);

    // I want to see that update is called with the right output of the modified metadata
    return await this.update(
      {
        id: id,
        metadata: modifiedMetadata
      },
      {
        where: {
          locale: metadata.locale
        }
      }
    );
  }
}

So with the above code sample, if I have an insance of Utut , I know I can easily mock update . 因此,使用上面的代码示例,如果我对Utut了解,我知道我可以轻松地模拟update I just don't know how to initialize the class to then mock things. 我只是不知道如何初始化该类,然后进行模拟。

The intent of the test I want to write is simply to see that update is called with the right parameters , based on my own code that has nothing to do with Sequelize. 我要编写测试的目的只是看基于我自己与Sequelize无关的代码, 使用正确的参数调用了update。 I want to make sure my transform on the inputs are set right when going to Sequelize. 我要确保在转到 Sequelize时正确设置了输入的变换。 I am not planning to check that the DB is actually updated. 我不打算检查数据库是否已实际更新。 The only problem I am having is actually instantiating the model. 我唯一遇到的问题实际上是实例化模型。

So all I need is for code like this to work: 所以我需要的是使这样的代码起作用:

const instance = new Utut(); // This line doesn't work
instance.update = Jest.fn();

I'm not sure why it says I need to invoke with new , when that is exactly what I am doing. 我不确定为什么它说我需要用new调用,而这正是我正在做的事情。

So taking advantage of the Model being returned from Sequelize.define causes this issue. 因此,利用从Sequelize.define返回的Model会导致此问题。 Rather than doing class Utut extends BaseUtut { , I did this: 而不是做class Utut extends BaseUtut { ,我这样做是:

// Static methods
Object.assign(BaseUtut, {});
// Instance methods
Object.assign(BaseUtut.prototype, {});

Doing that lets me do new on the model with no issue in my tests 这样一来,我就可以在模型上进行new的测试,而没有任何问题

Update 更新

Just saw this was unanswered on 9/23/19. 刚刚看到9/23/19尚未解决。 The above answer looks to be for an older version. 上面的答案似乎是针对旧版本的。 I now just do 我现在就做

class MyClass extends Sequelize.Model {}
MyClass.init(definition, options);
export default MyClass;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM