我试图为我的一个范围变量编写规范,但即使它们匹配也会失败。
控制台:
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"}]);
});
和我的控制员:
app.controller('ProductsIndexCtrl', ['$scope', 'ColorGroup', 'Shape', 'Material', function($scope, ColorGroup, Shape, Material) {
ColorGroup.query().$promise.then(function(data) {
$scope.color_groups = data;
});
发生了什么?如何匹配一组对象?控制台中的输出甚至表明它们匹配! toEqual
无效。
答案 0 :(得分:3)
我想出了一个使用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"}]));
答案 1 :(得分:1)
面临同样的问题。我发现这个解释很有用,它解决了我的问题。
AngularJS + Jasmine: Comparing objects
[toEqual进行深度平等比较。这意味着当对象的所有属性时,值相等,对象被认为是相等的。
正如您所说,您正在使用资源,它会为数组中的对象添加一些属性。
所以这个{id:12}变成了这个{id:12,$ then:function,$ resolved:true}这些不相等。如果您只是测试是否正确设置了值,则ID检查应该没问题。 ] 1