AngularJS Jasmine比较数组返回错误

AngularJS Jasmine comparing arrays returning error

本文关键字:返回 错误 数组 比较 Jasmine AngularJS      更新时间:2023-09-26

我试图在向myArray推送数据后测试它的值,但我总是收到数组内容与我的静态arr变量不相等的错误。有人能检查我下面的代码吗,让我知道我到底做错了什么,以及为什么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);