I'm trying to test a simple function (cloning an array) to learn how Jasmine works. 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? 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]);
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!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.