gpt4 book ai didi

angularjs - Jasmine:toEqual() 匹配器不适用于对象数组?

转载 作者:行者123 更新时间:2023-12-05 09:22:40 25 4
gpt4 key购买 nike

我正在尝试为我的范围变量之一编写规范,但即使它们匹配也失败了。

控制台:

  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' } ].

规范:

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"}]);
});

和我的 Controller :

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

这是怎么回事?如何匹配对象数组?控制台中的输出甚至显示它们匹配! toEqual 无效。

最佳答案

我想出了一个解决方法,使用 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"}]));

关于angularjs - Jasmine:toEqual() 匹配器不适用于对象数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25186431/

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