この仕様は、失敗したように見えますが、合格します。 (コードは角とレール上の本からです)
ここでは角アプリです:
var app = angular.module('customers',[]);
app.controller("CustomerSearchController",
["$scope", "$http", function($scope, $http) {
var page = 0;
$scope.customers = [];
$scope.search = function (searchTerm) {
if (searchTerm.length < 3) {
return;
}
$http.get("/customers.json",
{ "params": { "keywords": searchTerm, "page": page } }
).then(function(response) {
$scope.customers = response.data;
},function(response) {
alert("There was a problem: " + response.status);
}
);
};
}
]);
と、ここでジャスミンの仕様は次のとおりです。
describe("Error Handling", function() {
var scope = null,
controller = null,
httpBackend = null;
beforeEach(module("customers"));
beforeEach(inject(function ($controller, $rootScope, $httpBackend) {
scope = $rootScope.$new();
httpBackend = $httpBackend;
controller = $controller("CustomerSearchController", {
$scope: scope
});
}));
beforeEach(function() {
httpBackend.when('GET', '/customers.json?keywords=bob&page=0').respond(500, 'Internal Server Error');
spyOn(window, "alert");
});
it("alerts the user on an error", function() {
scope.search("bob");
httpBackend.flush();
expect(scope.customers).toEqualData([]);
expect(window.alert).toHaveBeenCalledWith(
"There was a problem: 500");
});
});
私はどのように理解していませんコントローラは$ httpBackendサービスにアクセスし、beforeEachメソッドで注入するために渡された無名関数に注入されます。 $ scopeサービスは渡されますが、httpBackendは渡されません。
ありがとうございます。わかった。 –