[英]Can't understand why an array is empty in Jasmine test
I'm trying to test a simple function (cloning an array) to learn how Jasmine works. 我正在尝试测试一个简单的函数(克隆数组)以了解Jasmine的工作原理。 The function I'm testing works (or not, it doesn't matter).
我正在测试的功能有效(或无效,没关系)。 But I don't know how to test it right.
但是我不知道如何正确测试。 This is the suite I've created:
这是我创建的套件:
describe("Clone an array of arrays", function(){
var array_element;
var array_origin;
var array_cloned;
beforeEach(function() {
array_element = [4,0];
array_origin = [1,2,array_element];
array_cloned= array_Clone(array_origin);
});
it("Clone the array", function() {
expect(array_cloned).toEqual(array_origin);
});
/* Commented because it is not relevant for the question
// We should clone, not copy references, to subarrays
if("Clone the elements", function(){
expect(array_cloned[2]).not.toBe(array_element);
});
*/
})
But, when I run it, I get the following error on the first test: 但是,当我运行它时,在第一次测试时出现以下错误:
Expected [ 1, 2, [ 4, 0 ] ] to equal [ ].
Why array_origin is an empty array? 为什么array_origin是一个空数组? I've been looking for information about this, but I can't understand why it doesn't have the value initialized at beforeEach() ([1,2,[3,4]);
我一直在寻找有关此信息,但我不明白为什么它没有在beforeEach()([1,2,[3,4]);
What is the correct way to test this? 什么是正确的测试方法?
Replace this: 替换为:
array_origin = [1,2,array_element]; // this will just put an array into an array
array_cloned= array_Clone(array_origin);
with this: 有了这个:
array_origin = [1,2].concat(array_element);
array_cloned = array_origin.map(function(elem) { return elem; }); // This is not a deep copy!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.