我想使用Karma-Jasmin测试我的代码,但是当我运行测试时,只有第一个被激活。我发现当我删除代码的某些部分时它会起作用,我会把它粘贴在底部。
这是我的整个Jasmine代码
describe("UserHandler", function(){
var el, scope, controller, configServiceSpy, myService, userDetails, myController, $httpBackend;
beforeEach(function () {
module('agenda');
beforeEach(inject(function(_$httpBackend_){
$httpBackend = _$httpBackend_;
}));
inject(function (userHandler) {
myService = userHandler;
myService.userDetails = {};
myService.userId = "P123";
myService.userDetails.expire_timestamp = 999999999999;
$httpBackend.expectGET('http://..../123').respond(200, {"foo" : "bar", "foo2": "bar2", "address": { "street":"some street" }})
myService.requestUserDetails();
userDetails = myService.userDetails;
});
inject(function($compile, $rootScope){
el = angular.element("<login-sidebar></login-sidebar>");
$compile(el)($rootScope.$new());
$rootScope.$digest();
scope = el.isolateScope() || el.scope() ;
});
});
afterEach(function(){
$httpBackend.verifyNoOutstandingExpectation();
$httpBackend.verifyNoOutstandingRequest();
});
it("true is true", function(){
$httpBackend.flush();
expect(true).toBe(true);
});
it("requestUserDetails", function(){
$httpBackend.flush();
expect(Object.keys(myService.userDetails).length).toBeGreaterThan(0);
});
it("getUserStreet not null", function(){
$httpBackend.flush();
expect(myService.userDetails.address.street).not.toBeNull();
});
xit("getUserStreet null", function(){
$httpBackend.flush();
expect(myService.userDetails.address.street).toBeNull();
});
});
这是控制台中的输出
Chrome 43.0.2357 (Mac OS X 10.10.3): Executed 1 of 4 (skipped 1) SUCCESS (0.079 secs / 0.07 secs)
因此,当我删除使用HTTP请求执行某些操作的部分时,它可以正常工作
当我删除这些代码行时
beforeEach(inject(function(_$httpBackend_){
$httpBackend = _$httpBackend_;
}));
inject(function (userHandler) {
myService = userHandler;
myService.userDetails = {};
myService.userId = "P123";
myService.userDetails.expire_timestamp = 999999999999;
$httpBackend.expectGET('http://..../123').respond(200, {"foo" : "bar", "foo2": "bar2", "address": { "street":"some street" }})
myService.requestUserDetails();
userDetails = myService.userDetails;
});
afterEach(function(){
$httpBackend.verifyNoOutstandingExpectation();
$httpBackend.verifyNoOutstandingRequest();
});
我做错了什么?为什么它不是仅执行所有测试的第一个测试(哪个测试是第一个测试并不重要。)