测试介绍
我已经得到了一个模块来使用,所以它通过了附加的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}]
测试环境
沙箱工作环境
附笔
:如果你能看看并解释一下我在这一点上做错了什么,或者什么也可能是错的,我会非常感激。