$httpBackend.verifyNoOutstandingExpectation() 的问题
Problems with $httpBackend.verifyNoOutstandingExpectation()
我最近开始使用 Karma + Karma-jasmine 编写单元测试,但我在以下测试中遇到问题:
describe("WEBSERVICE:", function () {
var webservice,
$httpBackend,
authRequestHandler,
webserviceURL = "http://localhost:8006/";
beforeEach(inject(function (Webservice, $injector) {
webservice = Webservice;
$httpBackend = $injector.get("$httpBackend");
authRequestHandler = $httpBackend
.when("GET", webserviceURL + "users/login")
.respond(200, "ok");
}));
afterEach(function() {
$httpBackend.verifyNoOutstandingExpectation();
$httpBackend.verifyNoOutstandingRequest();
});
it("should EXISTS", function () {
expect(webservice).toBeDefined();
});
it("should throw a WebserviceError if we are not logged in" , function () {
expect(function () {
webservice.item("negs", "RPT");
}).toThrow(webserviceAuthenticationError);
});
it("should NOT HAVE credentials when instantiated", function () {
expect(webservice.hasCredentials()).toBeFalsy();
});
it("should log in when valid credentials are given", function () {
$httpBackend.expectGET("users/login");
webservice.withCredentials("sam", "password");
});
});
似乎是以下内容造成了问题,因为当我删除它时所有测试都通过了:
afterEach(function() {
$httpBackend.verifyNoOutstandingExpectation();
$httpBackend.verifyNoOutstandingRequest();
});
我只是想知道是否有人可以帮助我解决这个问题。
非常感谢。
您遇到问题的原因是
$httpBackend.verifyNoOutstandingExpectation();
由于您上次测试
it("should log in when valid credentials are given", function () {
$httpBackend.expectGET("users/login");
webservice.withCredentials("sam", "password");
});
有未满足的请求,您可以在此看到 jsfiddle
Error: Unsatisfied requests: GET users/login
如果你注释掉
$httpBackend.verifyNoOutstandingExpectation()
您的前三个测试通过,但最后一个是琥珀色,因为没有预期,请参阅此 fiddle。
WEBSERVICE:
should EXISTS
should throw a WebserviceError if we are not logged in
should NOT HAVE credentials when instantiated
SPEC HAS NO EXPECTATIONS should log in when valid credentials are given
在 AngularJS documentation 中说
verifyNoOutstandingExpectation();
验证是否已发出通过预期 api 定义的所有请求。如果未发出任何请求,verifyNoOutstandingExpectation 将引发异常。
您需要重组该测试,以便
webservice.withCredentials("sam", "password");
通过$httpBackend
提出请求
我最近开始使用 Karma + Karma-jasmine 编写单元测试,但我在以下测试中遇到问题:
describe("WEBSERVICE:", function () {
var webservice,
$httpBackend,
authRequestHandler,
webserviceURL = "http://localhost:8006/";
beforeEach(inject(function (Webservice, $injector) {
webservice = Webservice;
$httpBackend = $injector.get("$httpBackend");
authRequestHandler = $httpBackend
.when("GET", webserviceURL + "users/login")
.respond(200, "ok");
}));
afterEach(function() {
$httpBackend.verifyNoOutstandingExpectation();
$httpBackend.verifyNoOutstandingRequest();
});
it("should EXISTS", function () {
expect(webservice).toBeDefined();
});
it("should throw a WebserviceError if we are not logged in" , function () {
expect(function () {
webservice.item("negs", "RPT");
}).toThrow(webserviceAuthenticationError);
});
it("should NOT HAVE credentials when instantiated", function () {
expect(webservice.hasCredentials()).toBeFalsy();
});
it("should log in when valid credentials are given", function () {
$httpBackend.expectGET("users/login");
webservice.withCredentials("sam", "password");
});
});
似乎是以下内容造成了问题,因为当我删除它时所有测试都通过了:
afterEach(function() {
$httpBackend.verifyNoOutstandingExpectation();
$httpBackend.verifyNoOutstandingRequest();
});
我只是想知道是否有人可以帮助我解决这个问题。 非常感谢。
您遇到问题的原因是
$httpBackend.verifyNoOutstandingExpectation();
由于您上次测试
it("should log in when valid credentials are given", function () {
$httpBackend.expectGET("users/login");
webservice.withCredentials("sam", "password");
});
有未满足的请求,您可以在此看到 jsfiddle
Error: Unsatisfied requests: GET users/login
如果你注释掉
$httpBackend.verifyNoOutstandingExpectation()
您的前三个测试通过,但最后一个是琥珀色,因为没有预期,请参阅此 fiddle。
WEBSERVICE:
should EXISTS
should throw a WebserviceError if we are not logged in
should NOT HAVE credentials when instantiated
SPEC HAS NO EXPECTATIONS should log in when valid credentials are given
在 AngularJS documentation 中说
verifyNoOutstandingExpectation();
验证是否已发出通过预期 api 定义的所有请求。如果未发出任何请求,verifyNoOutstandingExpectation 将引发异常。
您需要重组该测试,以便
webservice.withCredentials("sam", "password");
通过$httpBackend