gpt4 book ai didi

javascript - AngularJS Jasmine 比较数组返回错误

转载 作者:行者123 更新时间:2023-12-02 17:09:15 27 4
gpt4 key购买 nike

我试图在将数据推送到 myArray 后测试其值,但总是收到数组内容不等于我的静态 arr var 的错误。有人可以检查我下面的代码,让我知道我到底做错了什么以及为什么 Jasmine 报告两个数组不相等吗?谢谢

describe('Controller: MainCtrl', function () {

var MainCtrl, scope;

beforeEach(module('myApp'));

beforeEach(inject(function ($controller, $rootScope) {
scope = $rootScope.$new();
MainCtrl = $controller('MainCtrl', {
$scope: scope
});
}));

it('Final ordered array of equation elements', function () {
var arr = ["1", "2", "3", ".test[]"];
expect(scope.myArray).toBe(arr);
});


});



angular.controller('MainCtrl', function MainCtrl($scope) {

$scope.myArray = [];
$scope.myStr = '123.test[]';

$scope.myArray.push($scope.myStr.slice(0,1),
$scope.myStr.slice(1,2),
$scope.myStr.slice(2,3),
$scope.myStr.slice(3));
console.log(myArray); //returns ["1", "2", "3", ".test[]"]

});

最佳答案

toBe() 与 === 进行比较,这意味着它们需要是相同的对象而不仅仅是相同的值。您是否尝试过 expect(scope.myArray).toEqual(arr);

关于javascript - AngularJS Jasmine 比较数组返回错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24966230/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com