1

をルーティングアクティブ:

"use strict"; 

const BASE_API = APP_CONF.baseApi; 

export default function() { 
    "ngInject"; 

class API { 

constructor() { 
    this.loading = false; 
} 

logout() { 
    this.loading = true; 

    return $http.post(`${BASE_API}/logout`) 
    .then(res => { 
     this.loading = false; 

     return res.data; 
    }, err => { 
     this.loading = false; 

     throw err; 
    }); 
} 
} 

return new API(); 

}; 

私のテストを:

"use strict"; 

let baseApi = APP_CONF.baseApi; 

describe("Auth service", function() { 
let Auth; 

beforeEach(angular.mock.module("app")); 

beforeEach(angular.mock.inject(function(_Auth_) { 
    Auth = _Auth_; 
})); 

context("method LOGOUT", function() { 
    let $httpBackend; 

beforeEach(angular.mock.inject(function(_$httpBackend_) { 
    $httpBackend = _$httpBackend_; 
    $httpBackend.whenPOST(`${baseApi}/logout`).respond(200, { data: "ok"}); 
})); 

it("should change 'loading' flag", function() { 
    Auth.logout(); 
    assert.isTrue(Auth.loading); 
    $httpBackend.flush(); 
    assert.isFalse(Auth.loading); 
}); 

}); 
}); 

しかし、私のテストでは、次のエラーで失敗します。

Chrome 51.0.2704 (Linux 0.0.0) Auth service method LOGOUT should change 'loading' flag FAILED 
    Error: Unexpected request: GET http://localhost:3000/api/mailboxes 
    No more request expected 

このリクエストは経路が解決された後に呼び出されます:

"use strict"; 

export function config($locationProvider, $stateProvider, $urlRouterProvider) { 
    "ngInject"; 

    $urlRouterProvider.otherwise("404"); 
    $locationProvider.html5Mode(true); 

    $stateProvider 
    .state("404", { 
    url: "/404", 
    template: `<view-404 class="v-404" />` 
    }) 
    .state("home", { 
     url: "/", 
     template: `<view-home class="v-home" />`, 
     resolve: { 
     mailboxes: function(MailboxesApi, MailboxesStore, $state) { 
     "ngInject"; 

     return MailboxesApi.getAll() // THIS LINE ACTIVATES UNEXPECTED REQUEST 
     .then(mailboxes => { 
      MailboxesStore.set(mailboxes); 

      let inbox = MailboxesStore.getByName("inbox"); 

      if (inbox) { 
      $state.go("cabinet.mailbox", { mailboxid: inbox._id }); 
      } else { 
      $state.go("cabinet.mailbox", { mailboxid: MailboxesStore.getByIndex(0)._id }); 
      } 
     }); 
     } 
    } 
    }); 

    }; 

    export function run($transitions) { 
    "ngInject"; 

    $transitions.onError({ to: "*" }, (ErrorHandler, $error$) => { 
    "ngInject"; 

    ErrorHandler.handle($error$); 
    }); 

    }; 

私のテストで$ httpBackend.flush()が呼び出された後、状態 'home'がアクティブになっていることに気付きました。それはなぜ起こるのですか?

私の依存関係:

  • 角度-モック1.5.7
  • 角度1.5.6
  • 角度-UI-ルータ1.0.0-alpha.5
  • カルマ0.13.22
  • モカ2.5.3
  • チャイ3.5.0

答えて

2

エラーの詳細を調べた後、正常に実行されているように見えます。プロジェクトのルーティングロジックに従って、ホームビューにリダイレクトしてからフラッシュします(/ logout /)。しかし、それが家に到達したときには、メールボックスAPIと、もう1つのhttpbackend(GET)シミュレーションが必要です。

は、私はあなたのようなあなたのテストケースに似たような1行を追加する必要があります信じて

$httpBackend.whenGET('url').respond(200, { data: "ok"}); 
以下
関連する問題