2

I'm trying to write a spec for one of my scope variables, but it is failing even though they match.

Console:

  Expected [ { id: 1, name: 'Browns', created_at: '2014-08-06T21:38:41.000Z', updated_at: '2014-08-06T21:38:41.000Z' }, { id: 2, name: 'Blues', created_at: '2014-08-06T21:38:53.000Z', updated_at: '2014-08-06T21:38:53.000Z' }, { id: 3, name: 'Greens', created_at: '2014-08-06T21:39:03.000Z', updated_at: '2014-08-06T21:39:03.000Z' } ] to equal [ { id: 1, name: 'Browns', created_at: '2014-08-06T21:38:41.000Z', updated_at: '2014-08-06T21:38:41.000Z' }, { id: 2, name: 'Blues', created_at: '2014-08-06T21:38:53.000Z', updated_at: '2014-08-06T21:38:53.000Z' }, { id: 3, name: 'Greens', created_at: '2014-08-06T21:39:03.000Z', updated_at: '2014-08-06T21:39:03.000Z' } ].
    Error: Expected [ { id: 1, name: 'Browns', created_at: '2014-08-06T21:38:41.000Z', updated_at: '2014-08-06T21:38:41.000Z' }, { id: 2, name: 'Blues', created_at: '2014-08-06T21:38:53.000Z', updated_at: '2014-08-06T21:38:53.000Z' }, { id: 3, name: 'Greens', created_at: '2014-08-06T21:39:03.000Z', updated_at: '2014-08-06T21:39:03.000Z' } ] to equal [ { id: 1, name: 'Browns', created_at: '2014-08-06T21:38:41.000Z', updated_at: '2014-08-06T21:38:41.000Z' }, { id: 2, name: 'Blues', created_at: '2014-08-06T21:38:53.000Z', updated_at: '2014-08-06T21:38:53.000Z' }, { id: 3, name: 'Greens', created_at: '2014-08-06T21:39:03.000Z', updated_at: '2014-08-06T21:39:03.000Z' } ].

Spec:

describe('ProductsIndexCtrl', function() {
  beforeEach(inject(function($controller, $rootScope, $injector) {
    scope = $rootScope.$new();

    $httpBackend = $injector.get('$httpBackend');
    $httpBackend.when('/api/color_groups').respond([{"id":1,"name":"Browns","created_at":"2014-08-06T21:38:41.000Z","updated_at":"2014-08-06T21:38:41.000Z"},{"id":2,"name":"Blues","created_at":"2014-08-06T21:38:53.000Z","updated_at":"2014-08-06T21:38:53.000Z"},{"id":3,"name":"Greens","created_at":"2014-08-06T21:39:03.000Z","updated_at":"2014-08-06T21:39:03.000Z"}]);
    $httpBackend.when('/api/shapes').respond([{"id":1,"name":"Round","created_at":"2014-05-23T17:10:32.000Z","updated_at":"2014-05-23T17:10:32.000Z"},{"id":2,"name":"Cat Eye","created_at":"2014-05-27T18:53:36.000Z","updated_at":"2014-05-27T18:53:36.000Z"},{"id":3,"name":"sharps","created_at":"2014-05-28T21:41:44.000Z","updated_at":"2014-07-02T14:13:59.000Z"},{"id":4,"name":"square","created_at":"2014-06-02T15:29:58.000Z","updated_at":"2014-06-02T15:29:58.000Z"},{"id":5,"name":"Round","created_at":"2014-06-02T15:35:06.000Z","updated_at":"2014-06-02T15:35:06.000Z"},{"id":6,"name":"fffff","created_at":"2014-07-02T14:14:23.000Z","updated_at":"2014-07-02T14:14:23.000Z"},{"id":7,"name":"Rectangular","created_at":"2014-07-29T15:17:01.000Z","updated_at":"2014-07-29T15:17:01.000Z"},{"id":8,"name":"Diamond","created_at":"2014-07-29T15:17:08.000Z","updated_at":"2014-07-29T15:17:08.000Z"}]);
    $httpBackend.when('/api/materials').respond([{"id":1,"name":"Steel","created_at":"2014-05-23T17:04:42.000Z","updated_at":"2014-05-23T17:04:42.000Z"},{"id":2,"name":"Cotton","created_at":"2014-05-23T17:04:47.000Z","updated_at":"2014-05-23T17:04:47.000Z"},{"id":3,"name":"woods","created_at":"2014-05-28T20:26:16.000Z","updated_at":"2014-07-02T14:35:09.000Z"},{"id":4,"name":"plastic","created_at":"2014-06-02T15:30:05.000Z","updated_at":"2014-06-02T15:30:05.000Z"},{"id":5,"name":"glass","created_at":"2014-06-02T15:34:58.000Z","updated_at":"2014-06-02T15:34:58.000Z"},{"id":7,"name":"wood","created_at":"2014-06-02T16:51:47.000Z","updated_at":"2014-06-02T16:51:47.000Z"},{"id":8,"name":"rubber","created_at":"2014-07-02T14:35:51.000Z","updated_at":"2014-07-02T14:35:51.000Z"},{"id":9,"name":"carbon","created_at":"2014-07-29T14:52:44.000Z","updated_at":"2014-07-29T14:52:44.000Z"},{"id":10,"name":"paper","created_at":"2014-07-29T14:53:31.000Z","updated_at":"2014-07-29T14:53:31.000Z"},{"id":11,"name":"sandpaper","created_at":"2014-07-29T14:56:39.000Z","updated_at":"2014-07-29T14:56:39.000Z"},{"id":12,"name":"xfsdfsf","created_at":"2014-07-29T14:56:44.000Z","updated_at":"2014-07-29T14:56:44.000Z"}]);

    ProductsIndexCtrl = $controller('ProductsIndexCtrl', {
      '$scope': scope
    });

    $httpBackend.expectGET('/api/color_groups').respond([{"id":1,"name":"Browns","created_at":"2014-08-06T21:38:41.000Z","updated_at":"2014-08-06T21:38:41.000Z"},{"id":2,"name":"Blues","created_at":"2014-08-06T21:38:53.000Z","updated_at":"2014-08-06T21:38:53.000Z"},{"id":3,"name":"Greens","created_at":"2014-08-06T21:39:03.000Z","updated_at":"2014-08-06T21:39:03.000Z"}]);
    $httpBackend.expectGET('/api/shapes').respond([{"id":1,"name":"Round","created_at":"2014-05-23T17:10:32.000Z","updated_at":"2014-05-23T17:10:32.000Z"},{"id":2,"name":"Cat Eye","created_at":"2014-05-27T18:53:36.000Z","updated_at":"2014-05-27T18:53:36.000Z"},{"id":3,"name":"sharps","created_at":"2014-05-28T21:41:44.000Z","updated_at":"2014-07-02T14:13:59.000Z"},{"id":4,"name":"square","created_at":"2014-06-02T15:29:58.000Z","updated_at":"2014-06-02T15:29:58.000Z"},{"id":5,"name":"Round","created_at":"2014-06-02T15:35:06.000Z","updated_at":"2014-06-02T15:35:06.000Z"},{"id":6,"name":"fffff","created_at":"2014-07-02T14:14:23.000Z","updated_at":"2014-07-02T14:14:23.000Z"},{"id":7,"name":"Rectangular","created_at":"2014-07-29T15:17:01.000Z","updated_at":"2014-07-29T15:17:01.000Z"},{"id":8,"name":"Diamond","created_at":"2014-07-29T15:17:08.000Z","updated_at":"2014-07-29T15:17:08.000Z"}]);
    $httpBackend.expectGET('/api/materials').respond([{"id":1,"name":"Steel","created_at":"2014-05-23T17:04:42.000Z","updated_at":"2014-05-23T17:04:42.000Z"},{"id":2,"name":"Cotton","created_at":"2014-05-23T17:04:47.000Z","updated_at":"2014-05-23T17:04:47.000Z"},{"id":3,"name":"woods","created_at":"2014-05-28T20:26:16.000Z","updated_at":"2014-07-02T14:35:09.000Z"},{"id":4,"name":"plastic","created_at":"2014-06-02T15:30:05.000Z","updated_at":"2014-06-02T15:30:05.000Z"},{"id":5,"name":"glass","created_at":"2014-06-02T15:34:58.000Z","updated_at":"2014-06-02T15:34:58.000Z"},{"id":7,"name":"wood","created_at":"2014-06-02T16:51:47.000Z","updated_at":"2014-06-02T16:51:47.000Z"},{"id":8,"name":"rubber","created_at":"2014-07-02T14:35:51.000Z","updated_at":"2014-07-02T14:35:51.000Z"},{"id":9,"name":"carbon","created_at":"2014-07-29T14:52:44.000Z","updated_at":"2014-07-29T14:52:44.000Z"},{"id":10,"name":"paper","created_at":"2014-07-29T14:53:31.000Z","updated_at":"2014-07-29T14:53:31.000Z"},{"id":11,"name":"sandpaper","created_at":"2014-07-29T14:56:39.000Z","updated_at":"2014-07-29T14:56:39.000Z"},{"id":12,"name":"xfsdfsf","created_at":"2014-07-29T14:56:44.000Z","updated_at":"2014-07-29T14:56:44.000Z"}]);

  }));

  it('sets color_groups', function() {
    $httpBackend.flush();
    console.log(JSON.stringify(scope.color_groups)); // prints the same object!!!
    expect(scope.color_groups).toEqual([{"id":1,"name":"Browns","created_at":"2014-08-06T21:38:41.000Z","updated_at":"2014-08-06T21:38:41.000Z"},{"id":2,"name":"Blues","created_at":"2014-08-06T21:38:53.000Z","updated_at":"2014-08-06T21:38:53.000Z"},{"id":3,"name":"Greens","created_at":"2014-08-06T21:39:03.000Z","updated_at":"2014-08-06T21:39:03.000Z"}]);
  });

and my controller:

app.controller('ProductsIndexCtrl', ['$scope', 'ColorGroup', 'Shape', 'Material', function($scope, ColorGroup, Shape, Material) {
  ColorGroup.query().$promise.then(function(data) {
    $scope.color_groups = data;
  });

What's going on? How do I match an array of objects? The output in the console even shows that they match! toEqual is not working.

bigpotato
  • 26,262
  • 56
  • 178
  • 334

2 Answers2

5

I came up with a workaround using JSON.stringify();

expect(JSON.stringify(scope.color_groups)).toEqual(JSON.stringify([{"id":1,"name":"Browns","created_at":"2014-08-06T21:38:41.000Z","updated_at":"2014-08-06T21:38:41.000Z"},{"id":2,"name":"Blues","created_at":"2014-08-06T21:38:53.000Z","updated_at":"2014-08-06T21:38:53.000Z"},{"id":3,"name":"Greens","created_at":"2014-08-06T21:39:03.000Z","updated_at":"2014-08-06T21:39:03.000Z"}]));
bigpotato
  • 26,262
  • 56
  • 178
  • 334
  • This helped me debug my issue, while adapting the v2 jasmine introduction guide to v3, the examples of toEqual have the additional property of invocationOrder which I compare as jasmine.anything() – Tyeth Jun 08 '19 at 23:05
  • 1
    JSON.stringify does not guarantee order.https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify – amol Jul 11 '19 at 09:23
3

Faced the same problem. I found this explanation useful, and it solved my problem.

AngularJS + Jasmine: Comparing objects

[toEqual makes a deep equality comparison. Which means that when all the properties of the objects' values are equal, the objects are considered to be equal.

As you said, you are using resource which adds a couple of properties to the objects in the array.

So this {id:12} becomes this {id:12, $then: function, $resolved: true} which are not equal. Id checking should be fine if you are just testing if you set the values properly. ]1

Community
  • 1
  • 1
ng-darren
  • 1,022
  • 7
  • 11