当前位置: 首页 > 面试题库 >

AngularJS + Jasmine:比较对象

葛威
2023-03-14
问题内容

我刚刚开始为AngularJS应用编写测试,并且正在Jasmine中进行测试。

以下是相关的代码段

ClientController:

'use strict';

adminConsoleApp.controller('ClientController',
    function ClientController($scope, Client) {

        //Get list of clients
        $scope.clients = Client.query(function () {
            //preselect first client in array
            $scope.selected.client = $scope.clients[0];
        });

        //necessary for data-binding so that it is accessible in child scopes.
        $scope.selected = {};

        //Current page
        $scope.currentPage = 'start.html';

        //For Client nav bar
        $scope.clientNavItems = [
            {destination: 'features.html', title: 'Features'},
        ];

        //Set current page
        $scope.setCurrent = function (title, destination) {
            if (destination !== '') {
                $scope.currentPage = destination;
            }

        };

        //Return path to current page
        $scope.getCurrent = function () {
            return 'partials/clients/' + $scope.currentPage;
        };

        //For nav bar highlighting of active page
        $scope.isActive = function (destination) {
            return $scope.currentPage === destination ? true : false;
        };

        //Reset current page on client change
        $scope.clientChange = function () {
            $scope.currentPage = 'start.html';
        };
    });

ClientControllerSpec:

'use strict';

var RESPONSE = [
    {
        "id": 10,
        "name": "Client Plus",
        "ref": "client-plus"
    },
    {
        "id": 13,
        "name": "Client Minus",
        "ref": "client-minus"
    },
    {
        "id": 23805,
        "name": "Shaun QA",
        "ref": "saqa"
    }
];

describe('ClientController', function() {

    var scope;

    beforeEach(inject(function($controller, $httpBackend, $rootScope) {
        scope = $rootScope;
        $httpBackend.whenGET('http://localhost:3001/clients').respond(RESPONSE);
        $controller('ClientController', {$scope: scope});
        $httpBackend.flush();
    }));

    it('should preselect first client in array', function() {
        //this fails.
        expect(scope.selected.client).toEqual(RESPONSE[0]);
    });

    it('should set current page to start.html', function() {
        expect(scope.currentPage).toEqual('start.html');
    });
});

测试失败:

Chrome 25.0 (Mac) ClientController should preselect first client in array FAILED
    Expected { id : 10, name : 'Client Plus', ref : 'client-plus' } to equal { id : 10, name : 'Client Plus', ref : 'client-plus' }.
    Error: Expected { id : 10, name : 'Client Plus', ref : 'client-plus' } to equal { id : 10, name : 'Client Plus', ref : 'client-plus' }.
        at null.<anonymous> (/Users/shaun/sandbox/zong-admin-console-app/test/unit/controllers/ClientControllerSpec.js:43:39)

有谁知道为什么会发生这种情况吗?

另外..由于我不熟悉AngularJS测试,因此欢迎对我的测试设置错误还是可以改进提出任何意见。

更新:

包括ClientService:

'use strict';

AdminConsoleApp.services.factory('Client', function ($resource) {
    //API is set up such that if clientId is passed in, will retrieve client by clientId, else retrieve all.
    return $resource('http://localhost:port/clients/:clientId', {port: ':3001', clientId: '@clientId'}, {

    });
});

另外,我通过比较id解决了这个问题:

it('should preselect first client in array', function () {
    expect(scope.selected.client.id).toEqual(RESPONSE[0].id);
});

问题答案:

toEqual进行深入的平等比较。这意味着,当对象值的所有属性都相等时,则认为对象相等。

如您所说,您正在使用资源,该资源向数组中的对象添加了几个属性。

因此这{id:12}成为{id:12, $then: function, $resolved: true}不相等的。如果您只是在测试是否正确设置了值,则ID检查应该可以。



 类似资料:
  • 本文向大家介绍AngularJS equal比较对象实例详解,包括了AngularJS equal比较对象实例详解的使用技巧和注意事项,需要的朋友参考一下 使用情况 1 首先,所有满足 a === 3 这种的对象,在angular.equals(a,b)中都会返回真 2 所有对象的类型,以及属性值都相同的,也会返回真 3 NaN和NaN也会返回真(在javascript中,返回的是假) 4 正则也

  • 我正在尝试创建一个二进制搜索程序,该程序可以使用各种类型的变量(int、float、string等)来查看数组中是否存在元素。我正试图找出如何比较变量。下面是我正在使用的内容的草图: 我甚至不确定使用对象是否是最好的方法。

  • 问题内容: 我必须将对象与预定义类的列表进行比较。 使用安全还是应该使用? 注意: 我不能使用,我没有对象,我只有对象。在这种情况下,我会像枚举一样使用它! 问题答案: 不会覆盖来自的方法,该方法是这样实现的: 因此与(除非为null)相同。

  • 在爪哇中。如果我们必须将一个对象与另一个对象进行比较。我们比较该对象中的每个字段。 学生 1 对象具有标记 1、标记 2、标记 3、名称、年龄作为字段。学生 2 对象具有标记 1、标记 2、标记 3、名称、年龄作为字段。因此,要检查2名学生是否相等...我们比较每个字段。 但是,如果 Student 对象有许多字段,该怎么办?学生1对象有标记1,标记2,标记3,名称,年龄,地址,颜色,类,国家,部

  • 问题内容: 我需要编写一个比较器,它采用类型A的对象A和类型B的对象B。这两个对象不是公共对象的扩展。它们的确不同,但是我需要通过其中的通用字段来比较这两个对象。我必须使用比较器接口,因为对象存储在Set中,并且在必须对CollectionUtils执行操作之后。我在Google上搜索了一下,发现了Comparator的解决方案,但只有相同的类型。 我试图朝这个方向实施思考,但是我不知道我是否在正

  • 我需要写一个比较器,取一个a类型的对象a和一个B类型的对象B。这两个对象不是一个公共对象的扩展。他们确实是不同的,但我需要比较这两个对象在它的共同领域。我必须使用比较器接口,因为对象存储在Set中,之后我必须使用CollectionUtils进行操作。我搜索了一点点,我用比较器找到了解决方案,但只有相同的类型。 TXS 附注:我在不同的集合中添加两个对象: 之后我会这样想: