$ window.location的jasmine单元测试

时间:2016-05-11 19:25:03

标签: angularjs jasmine karma-jasmine

我是茉莉花的新手,可以用一些hwlp为下面的代码编写茉莉花单元测试:

otherwise({redirectTo:function(){
    if(CONTEXTUAL_START_VIEW) {
        return ($window.location.href.indexOf('/flight/') || $window.location.href.indexOf('/air/')) > -1  ? '/air' : '/hotel';
    }
    else {
        return '/hotel';
    }
}});

我试过以下方式:

describe("Routing to air home", function() {
    var scope, element, compile, $window, $location;
    var windowObj = {location: {href: {indexOf: `jasmine.createSpy()}}};`
    $location = jasmine.createSpyObj('$location', ['path']);
    beforeEach(module(function($provide) {
        $provide.value('$window', windowObj);
    }));
    beforeEach(module('hotelApp.routing'));
    beforeEach(inject(function($compile, $rootScope, _$window_) {
        scope = $rootScope.$new();
        $window = _$window_;
  }));
it("should redirect to /air when /flight/ or /air/ otherwise to default hotel", function() {
   scope.CONTEXTUAL_START_VIEW = true;
   $location.path("/");
   scope.$digest();
   expect($location).toBeDefined();
   expect(windowObj.location.href.indexOf('/flight')).toBeUndefined();
   expect($location.path()).toEqual();
   });
});

0 个答案:

没有答案