javascript - Jasmine 测试...测试失败但条件成立

标签 javascript unit-testing jasmine karma-runner karma-jasmine

您好,我正在尝试为名为 daterangecontroller 的 Controller 编写一些代码,该 Controller 调用服务为其提供一些开始日期和结束日期的值(这些是日历小部件的一部分)。

日期范围服务提供开始和结束日期的默认值。然后 Controller 监视开始和结束日期模型的更改。当发生更改时,它会调用该服务来更新日期范围间隔,最终由另一个服务调用。

如果错误显示以下内容,我很难理解为什么此条件会失败:

Chrome 35.0.1916 (Mac OS X 10.9.2) Controller: dateRangeCtrl default start date loads as     8 days ago FAILED
Expected Date(Wed Jun 11 2014 11:04:11 GMT-0700 (PDT)) to be Date(Wed Jun 11 2014 11:04:11 GMT-0700 (PDT)).
Error: Expected Date(Wed Jun 11 2014 11:04:11 GMT-0700 (PDT)) to be Date(Wed Jun 11 2014 11:04:11 GMT-0700 (PDT)).
    at null.<anonymous> (/Users/test/client/spec/controllers/dateRangeTest.js:32:28)

这是我的代码的相关部分。

Controller :

angular.module("vizApp").controller("DaterangeCtrl", function ($rootScope, $scope, DateRangeService) {
"use strict";

// Dates to fill in the calendar widget
var dates_current_interval = DateRangeService.giveCurrentInterval();
var dates_array = dates_current_interval.split("/");
$scope.startDate = new Date(dates_array[0]);
$scope.endDate = new Date (dates_array[1]);
var dateRange = {};

// this model watches for changes in the "from" calendar section
$scope.$watch("startDate", function (newVal, oldVal, scope) {

    if (newVal !== oldVal && newVal !== null && $scope.startDate) {

        dateRange.start = new Date($scope.startDate);
        dateRange.end = new Date($scope.endDate);


        return DateRangeService.updateDateInterval(dateRange);

    }
});

// this model watches for changes in the "to" calendar section
$scope.$watch("endDate", function (newVal, oldVal, scope) {

    if (newVal !== oldVal && newVal !== null && $scope.startDate) {

        dateRange.start = new Date($scope.startDate);
        dateRange.end = new Date($scope.endDate);

        return DateRangeService.updateDateInterval(dateRange);


    }
});

});

还有服务

 angular.module("vizApp").factory("DateRangeService", [ "$rootScope", function () {
"use strict";
return{

    dateInterval: null,

    /**
     *
     * @param : none
     * returns either a default to fill in the input boxes or the updated dateInterval
     *
     */
    giveCurrentInterval: function giveCurrentInterval(){
        if (this.dateInterval === null){
            var startDate = new Date();
            startDate.setDate(startDate.getDate() - 8);
            var endDate = new Date();
            var dateFill = startDate.toISOString() + "/" + endDate.toISOString();
            return dateFill;
        }
        else{
            return this.dateInterval;
        }
    },

    /**
     *
     * @param dateRange : the date range passed in from the model being watched
     * returns the new dateInterval
     *  
     *
     */
    updateDateInterval: function updateDateInterval(dateRange){

        this.dateInterval = dateRange.start.toISOString() + "/" + dateRange.end.toISOString(); 
        return this.dateInterval;
    }



};
}]);

更新:这是 Jasmine 代码

"use strict";

describe("Controller: dateRangeCtrl", function () {

    var scope, DateRangeController, httpBackend;


    //load the controller's module
    beforeEach(module('vizApp'));
    beforeEach(function () {
                        angular.mock.inject(function ($injector) {
                            httpBackend = $injector.get("$httpBackend");

                        });
                    });

    //initialize the controller and a mock scope
    beforeEach(inject(function($controller, $rootScope) {
        // create a new child scope
        scope = $rootScope.$new();
        //create a new instance of date range controller
        DateRangeController = $controller('DaterangeCtrl', { $scope : scope });

    }));

    it('default start date loads as 8 days ago', function(){
        var mockStartDate = new Date();
        mockStartDate.setDate(mockStartDate.getDate() - 8);

        httpBackend.expectGET('partials/landing.html');
        httpBackend.whenGET('partials/landing.html').respond(200);
        expect(scope.startDate).toBe(mockStartDate);

    });



    it('changes date range when start date is changed', function(){
        var mockStartDate2 = new Date();
        mockStartDate2.setDate(mockStartDate2.getDate() - 10);

        httpBackend.expectGET('partials/landing.html');
        httpBackend.whenGET('partials/landing.html').respond(200);

        scope.$apply(function () {
            scope.startDate = mockStartDate2;
        });

        expect(scope.startDate).toBe(mockStartDate2);

    });
});

最佳答案

使用 toEqual(),而不是 toBe(),toEqual 检查等价性,toBe 检查它们是否是完全相同的对象。

如果您有大量测试,这可能会非常慢,我建议将其分开。

关于javascript - Jasmine 测试...测试失败但条件成立,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24313518/

相关文章:

c++ - 设置单元测试需要多少个项目

java - 单元测试-Mockito和Butterknife-如何 mock

coffeescript - 无法运行最简单的规范 : TypeError: '[object Object]' is not a constructor (evaluating 'new Options()' )

unit-testing - 单元测试 Angular 右键单击指令

javascript - 拖放数据时清除输入字段

javascript - 如何以 Angular 使用functions.php文件中的函数

python - 我可以从不同的目录运行 django 测试 (manage.py) 吗?

typescript - 用于 TypeScript 应用程序测试的 Angular 2 - 规范在 jasmine 中出现多次

javascript - Webpack 找不到 Typescript 类型解析所需的导入。如何解决这个问题?

javascript - vuejs2 onclick不会调用定义的方法函数