最近,我已经开始使用Karma +Karma编写单元测试,但是我在以下测试中遇到了问题:
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();
});我只是想知道是否有人能帮我这个忙。非常感谢。
发布于 2015-04-30 20:00:45
你有问题的原因是
$httpBackend.verifyNoOutstandingExpectation();是因为你最后一次考试
it("should log in when valid credentials are given", function () {
$httpBackend.expectGET("users/login");
webservice.withCredentials("sam", "password");
});有不满意的请求,您可以在这个小提琴中看到
Error: Unsatisfied requests: GET users/login如果你评论掉
$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在“AngularJS 文档”中,它说
verifyNoOutstandingExpectation();
验证通过expect api定义的所有请求是否都已发出。如果没有发出任何请求,verifyNoOutstandingExpectation将抛出一个异常。
您将需要重组该测试,以便
webservice.withCredentials("sam",“密码”);
通过$httpBackend发出请求
https://stackoverflow.com/questions/29971793
复制相似问题