Sequelize-test-helpers 和 calledWith AssertionError 未定义参数

Posted

技术标签:

【中文标题】Sequelize-test-helpers 和 calledWith AssertionError 未定义参数【英文标题】:Sequelize-test-helpers and calledWith AssertionError undefined argument 【发布时间】:2021-12-04 14:11:41 【问题描述】:

我是 sinon, chai and mocha 测试的新手 我正在使用这三个测试库和包sequelize-test-helpers

我正在尝试运行测试以检查 belongsTo 关联是否正常工作,这是我采取的步骤:

首先是我使用 sequelize 的数据库中的实际表,数据库是 Postgres

module.exports = (sequelize, DataTypes) => 
  // const  DataTypes  = Sequelize;
  const DealerProduct = sequelize.define('DealerProduct', 
    dpid: 
      type: DataTypes.UUID,
      defaultValue: DataTypes.UUIDV4,
      allowNull: false,
      primaryKey: true
    ,
    product_name: 
      type: DataTypes.STRING(100),
      allowNull: false,
      unique: true,
      // validate: 
      //   len: [1, 100]
      // 
    ,
    type: 
      type: DataTypes.STRING(25),
      allowNull: true,
      // validate: 
      //   len: [1, 100]
      // 
    ,
    description: 
      type: DataTypes.TEXT,
      allowNull: true
    ,
    price: 
      type: DataTypes.DECIMAL(10, 2),
      allowNull: false
    ,
    quantity: 
      type: DataTypes.INTEGER,
      allowNull: false
    
  , 
    tableName: 'dealer_products',
    timestamps: true,
    createdAt: 'created_at',
    updatedAt: 'updated_at',
    // freezeTableName: true
    // paranoid: true
  );

  DealerProduct.associate = models =>  // **For starters this is the belongsTo association I would like to make my test for**
    DealerProduct.belongsTo(models.Dealer,  
      foreignKey: 
        type: DataTypes.UUID,
        allowNull: false,
        name: "dealers_did",
      
    );

    DealerProduct.hasOne(models.StoreProduct, 
      foreignKey: 
        type: DataTypes.UUID,
        allowNull: false,
        name: 'dealer_product_dpid'
      
    )
  ;

  return DealerProduct;

这是测试

const 
    sequelize,
    dataTypes,
    checkModelName,
    checkPropertyExists,
    checkUniqueIndex
 = require('sequelize-test-helpers');
const chai = require("chai");
const sinon = require("sinon");
const sinonChai = require("sinon-chai");
chai.should();
chai.use(sinonChai)

const DealerProductModel = require('../../models/dealer-products-model');

describe('server/models/all', async () => 

  describe('server/models/dealer-products-model', async () => 
    
    const DealerProduct = DealerProductModel(sequelize, dataTypes);
    const dealerProduct = new DealerProduct();

    checkModelName(DealerProduct)('DealerProduct');

    describe('check all properties exist', () => 
        ['dpid', 'product_name', 'type', 'description', 'price', 'quantity'].forEach(checkPropertyExists(dealerProduct))
    )

    describe('check associations', () => 
      const OtherModel = 'Dealer' // it doesn't matter what
      before(() => 
        DealerProduct.associate(  OtherModel   )
      )
      it('defined a belongsTo association with Dealer', () => 
        chai.expect(DealerProduct.belongsTo).to.have.been.calledWith(OtherModel, 
          foreignKey: 
            type: dataTypes.UUID,
            allowNull: false,
            name: 'dealer_product_dpid'
          
        )
      )
    )
  )

)

我是从 page 开始关注这个例子的

我不知道如何将 sinon 与 chai 链接,以便 calledWith 可以工作,但我四处搜索发现在导入它们时会这样做(可能我在这里错了或遗漏了一些东西):

const chai = require("chai");
const sinon = require("sinon");
const sinonChai = require("sinon-chai");
chai.should();
chai.use(sinonChai)

它可以让calledWith 工作,因为我正在窥探通过这个expect 方法查看关联:

    describe('check associations', () => 
      const OtherModel = 'Dealer' // it doesn't matter what
      before(() => 
        DealerProduct.associate(  OtherModel   )
      )
      it('defined a belongsTo association with Dealer', () => 
        chai.expect(DealerProduct.belongsTo).to.have.been.calledWith(OtherModel, 
          foreignKey: 
            type: dataTypes.UUID,
            allowNull: false,
            name: 'dealer_product_dpid'
          
        )
      )
    )

我收到此错误:

     AssertionError: expected belongsTo to have been called with arguments 'Dealer', 
  foreignKey: 
    type: [Function: Noop],
    allowNull: false,
    name: 'dealer_product_dpid'
  

undefined '"Dealer"' 

  foreignKey:  type: [Function: Noop], allowNull: false, name: 'dealers_did' 
  foreignKey: 
    type: [Function: Noop],
    allowNull: false,
    name: 'dealer_product_dpid'
  

这是说undefined '"Dealer"' 经销商我的字符串未定义。我该如何解决这个问题?

对于原始表,经销商是与 DealerProduct 关联的表

【问题讨论】:

【参考方案1】:

回答

除了

,我所有的步骤都是正确的
      const OtherModel = 'Dealer' // it doesn't matter what
      before(() => 
        DealerProduct.associate(  OtherModel   )
      )
calledWith(OtherModel)

calledWith 不能有字符串参数,而是模型 Dealer。

所以我在做什么:

const DealerProductModel = require('../../models/dealer-products-model');
    const DealerProduct = DealerProductModel(sequelize, dataTypes);
    const dealerProduct = new DealerProduct();

我也应该导入 DealerModel,所以它应该是

const DealerProductModel = require('../../models/dealer-products-model');
const DealerModel = require('../../models/dealers-model');
    const DealerProduct = DealerProductModel(sequelize, dataTypes);
    const dealerProduct = new DealerProduct();
    const Dealer = DealerModel(sequelize, dataTypes);
    const dealer = new Dealer();

然后像这样发送const Dealer = DealerModel(sequelize, dataTypes);

    describe('check associations', () => 
      before(() => 
        DealerProduct.associate(  Dealer   )
      )
      it('defined a belongsTo association with Dealer', () => 
        chai.expect(DealerProduct.belongsTo).to.have.been.calledWith(Dealer)
      )
    )

【讨论】:

以上是关于Sequelize-test-helpers 和 calledWith AssertionError 未定义参数的主要内容,如果未能解决你的问题,请参考以下文章

第三十一节:扫盲并发和并行同步和异步进程和线程阻塞和非阻塞响应和吞吐等

shell中$()和 ` `${}${!}${#}$[] 和$(()),[ ] 和(( ))和 [[ ]]

Java基础8---面向对象代码块和继承和this和super和重写和重载和final

Java基础8---面向对象代码块和继承和this和super和重写和重载和final

JS中some()和every()和join()和concat()和pop(),push(),shift(),unshfit()和map()和filter()

malloc和free,brk和sbrk和mmap和munmap的使用和关系以及内存分配的原理