次のコントローラーがあります (インスタンス化時に を明示的に呼び出すことに注意して$scope.getNotifications()ください)。
bla.controller("myctrl", [
"$scope", "$http", "configs", function ($scope, $http, configs) {
$scope.getNotifications = function () {
$http.get("bla/blabla").success(function (data) {
});
};
$scope.removeNotification = function (notification) {
var index = $scope.allNotifications.indexOf(notification);
$scope.allNotifications.splice(index, 1);
};
$scope.getNotifications();
}
]);
次に、いくつかの単体テストを行います (コントローラーがそれぞれの前にインスタンス化されることに注意してください)。
describe("blaController", function () {
var scope, $httpBackend;
beforeEach(module('bla'));
beforeEach(inject(function ($controller, $rootScope, _$httpBackend_) {
scope = $rootScope.$new();
$httpBackend = _$httpBackend_;
$controller('blaCtrl', { $scope: scope });
}));
afterEach(function(){
//assert
$httpBackend.verifyNoOutstandingExpectation();
$httpBackend.verifyNoOutstandingRequest();
});
it("should get all notifications from server when instantiated", function () {
//arrange
$httpBackend.expectGET("api/v1/notifications").respond(200, {});
$httpBackend.flush();
//act - done implicitly when controller is instantiated
});
it("should store all notifications from server on the client when success call to server", function () {
//arrange
$httpBackend.whenGET("api/v1/notifications").respond(200, [{ a: 1, b: 2 }, { c: 3, d: 4 }]);
$httpBackend.flush();
//act - done implicitly when controller is instantiated
//assert
expect(scope.allNotifications).toEqual([{ a: 1, b: 2 }, { c: 3, d: 4 }]);
});
今まではすべて順調です。すべてのテストに合格します。しかし、HTTP 呼び出しを必要としない新しいテスト (以下を参照) を追加すると失敗しafterEach()ますremoveNotification()。これはカルマからのエラー メッセージです:
PhantomJS 1.9.7 (Windows 8) notificationCenterController removeNotification should remove the given notification from the list FAILED Error: Unexpected request: GET api/v1/notifications No more request expected
it("should remove the given notification from the list", function () {
//arrange
var targetObj = { a: 2 };
scope.allNotifications = [{ a: 1 }, targetObj, { a: 3 }];
//act
scope.removeNotification(targetObj);
//assert
expect(scope.allNotifications).toEqual([{ a: 1 }, { a: 3 }]);
});
私のテストのほとんどには http 呼び出しがあるため、afterEach に検証を配置することは理にかなっています。N-1 テストで afterEach ボディをコピーして貼り付けるのを避けるために、他にどのようなオプションが必要か疑問に思っていました。電話を無視するように指示する方法はありますか?$httpBackend