Jasmine $httpBackend.VerifyNoOutstandingExpection()存在问题
我最近开始使用Karma+Karma jasmine编写单元测试,但我在以下测试中遇到了问题:Jasmine $httpBackend.VerifyNoOutstandingExpection()存在问题,jasmine,karma-runner,karma-jasmine,jasmine-node,Jasmine,Karma Runner,Karma Jasmine,Jasmine Node,我最近开始使用Karma+Karma jasmine编写单元测试,但我在以下测试中遇到了问题: describe("WEBSERVICE:", function () { var webservice, $httpBackend, authRequestHandler, webserviceURL = "http://localhost:8006/"; beforeEach(inject(function (Webservic
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");
});
有未满足的要求,你可以在这里看到
如果你不加评论的话
$httpBackend.verifyNoOutstandingExpectation()
您的前三个测试通过,但最后一个测试是琥珀色的,因为没有预期,请参见此
在AngularJS中它说
验证无超越期望()
验证是否已发出通过expect api定义的所有请求。如果未发出任何请求,VerifyNoOutstandingExpection将抛出异常
您需要重新构造该测试,以便
webservice.withCredentials(“sam”,“password”)
通过
$httpBackend
发出请求非常感谢,我不知道如何获得webservice.withCredentials(“sam”,“password”)
通过$httpBackend
发出请求,尽管…$httpBackend是一个伪HTTP后端实现,适用于使用$HTTP服务的单元测试应用程序。因此,您将在webService对象/服务中使用$http.get,并使用$httpBackend进行模拟和测试。看看文档中的示例,实际上您需要调用$httpBackend.flush()来解决挂起的请求,这也会评估期望值
$httpBackend.verifyNoOutstandingExpectation()
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