代码之家  ›  专栏  ›  技术社区  ›  volna

测试模块根据测试环境产生不同的输出

  •  0
  • volna  · 技术社区  · 6 年前

    测试介绍

    我已经得到了一个模块来使用,所以它通过了附加的jest测试。目前,我坚持以下测试规则:

    describe(`TWO TIMES the SAME product ADDED`, () => {
      const _VATRate = 20;
      const _product = {
        name: 'product1',
        price: 1.50,
        quantity: 1
      };
      let _instance;
      let _cart;
    
      beforeEach(() => {
        _instance = window[NAMESPACE][MODULE_NAME]();
        _instance.init(_VATRate);
    
        _cart = _instance.addProducts([_product, _product]);
      });
    
      test(`cart's products`, () => {
        const result = Object.assign({}, _product, {'quantity': 2});
        expect(_cart.products).toEqual([result]);
      });
    })
    

    期望值为: [{"name": "product1", "price": 1.5, "quantity": 2}]


    浏览器中的实现模块(Works)

    function CartModule() {
      var _Cart = {
        total: {
          beforeVAT: 0,
          afterVAT: 0,
          VAT: 0
        },
        products: [],
        VATRate: null
      };
    
      var _getCart = function() {
        return {
          total: _Cart.total,
          products: _Cart.products
        };
      };
    
      var _updateTotalPrice = function() {
        let invoice = _Cart.total;
    
        invoice.beforeVAT = 0;
        invoice.afterVAT = 0;
        invoice.VAT = 0;
        let roundDecimals = number => +number.toFixed(2);
    
        _Cart.products.forEach(product => {
          invoice.beforeVAT = roundDecimals(
            product.price * product.quantity + invoice.beforeVAT
          );
        });
    
        invoice.VAT = roundDecimals(_Cart.total.beforeVAT / 100 * _Cart.VATRate);
        invoice.afterVAT = invoice.VAT + invoice.beforeVAT;
      };
    
      return {
        init: function(VATRate) {
          return (_Cart.VATRate = VATRate || 0);
        },
    
        getCart: _getCart,
    
        addProducts: function(recievedProducts) {
          let products = Array.from(arguments),
            updatedProduct,
            cartProducts = _getCart().products,
            existingProduct;
    
          products.forEach(product => {
            existingProduct = cartProducts.findIndex(
              existing => existing.name === product.name
            );
    
            if (existingProduct >= 0) {
              updatedProduct = cartProducts[existingProduct];
              updatedProduct.quantity++;
              cartProducts[existingProduct] = updatedProduct;
            } else {
              updatedProduct = product;
              updatedProduct.quantity = 1;
              cartProducts.push(product);
            }
          });
    
          // Update Total Price
          _updateTotalPrice();
          return _getCart();
        },
    
        changeProductQuantity: function(product, newQuantity) {
          let products = _Cart.products,
            productIndex = products.findIndex(
              existing => existing.name === product.name
            );
    
          products[productIndex].quantity = +newQuantity;
          _updateTotalPrice();
    
          return _getCart();
        },
    
        removeProducts: function(product) {
          let products = _Cart.products,
            productIndex = products.findIndex(
              existing => existing.name === product.name
            );
    
          products.splice(productIndex, 1);
          _updateTotalPrice();
    
          return _getCart();
        },
    
        destroy: function() {
          /* Module = null */
        }
      };
    }
    
    var shoppingCart = CartModule(),
        _product = {
          name: 'product1',
          price: 1.50,
          quantity: 1
        };
    
    
    shoppingCart.init(20);
    shoppingCart.addProducts(_product, _product)
    
    console.log(shoppingCart.getCart().products)

    笑话测试
    但是在测试环境中情况是不同的。除此之外 sandbox local 版本不同:

    本地版本

    Expected value to equal:
          [{"name": "product1", "price": 1.5, "quantity": 2}]
        Received:
          [{"name": "product1", "price": 1.5, "quantity": 1}, {"0": {"name": "product1", "price": 1.5, "quantity": 1}, "1": {"name": "product1", "price": 1.5, "quantity": 1}, "quantity": 1}]
    

    沙盒版本输出为:

    Expected value to equal:
      [{"name": "product1", "price": 1.5, "quantity": 2}]
    Received:
      [{"0": {"name": "product1", "price": 1.5, "quantity": 1}, "1": {"name": "product1", "price": 1.5, "quantity": 1}, "quantity": 1}]
    


    测试环境
    沙箱工作环境
    enter image description here enter image description here

    Edit zx9wy9l9qm

    附笔 :如果你能看看并解释一下我在这一点上做错了什么,或者什么也可能是错的,我会非常感激。

    1 回复  |  直到 6 年前
        1
  •  1
  •   Leroy    6 年前

    我想是这句话:

    let products = Array.from(arguments),
    

    这将产生一个产品阵列阵列,由于这一行:

    _cart = _instance.addProducts([_product, _product]);
    

    你可能想用这样的东西:

    addProducts: function(recievedProducts) {
          let products = recievedProducts,
    

    如果你的 addProducts 可以将两个对象都作为数组处理,您可能需要执行以下操作:

    addProducts: function(recievedProducts) {
          let products = Array.isArray(recievedProducts) ? receivedProducts : [receivedProducts],
    
    推荐文章