Jasmine测试无法识别URL更改

时间:2016-02-23 01:03:23

标签: angularjs jasmine karma-jasmine

这是我的base.js

self.logout = function(callback) {
     $http.post("api/logout")
     .then(function(response) {
         $window.location.href = "/";
     }, function(response) {
         self.accessErrors(response.data);
         callback();
      });
 };

这是我的test_base.js

it('logout() should POST to api/logout and redirect on success', function() {
    // I want to test if the logout(cb) function posts to api/logout and, on success, redirect to /
    $loc.path('/logout');
    mockBackend.expectPOST("api/logout").respond(204, {msg: 'No Content'});

    var cb = jasmine.createSpy('cb');
    BaseService.logout(cb);

    mockBackend.flush();
    expect($loc.path()).toEqual('/');
});

但是当我测试它时,我收到一个错误。错误说:

Expected '/logout' to equal '/'.

知道为什么它没有抓住该网址改为' /'当这一行被执行时:

$window.location.href = "/";

2 个答案:

答案 0 :(得分:0)

您确定$ http.post会使用window.location.href跳转到该函数吗?

尝试

$window.location.href = "#/";

答案 1 :(得分:0)

因为在我的退出功能中我使用$window.location.path来更改网址,所以我将其设为test_base.js

it('logout() should POST to api/logout and redirect on success', function() {
    // I want to test if the logout(cb) function posts to api/logout and, on success, redirect to /
    $window.location.href = '/logout';
    mockBackend.expectPOST("api/logout").respond(204, {msg: 'No Content'});

    var cb = jasmine.createSpy('cb');
    BaseService.logout(cb);

    mockBackend.flush();
    expect($window.locaiton.href).toEqual('/');
});