Javascript 茉莉花没有承诺
我不认识茉莉花。我试图让我的工厂承诺得到解决,但它似乎从来没有解决 这是我的工厂,它将收回一份公司名单 还有我的规格 正如您所看到的,我使用$rootScope.$digest来运行promise,但是我的期望从未达到,因此测试总是通过 我读过很多关于这方面的帖子,他们都说这应该行得通。我是做错了什么,还是错过了什么 编辑-简化方法 因此,在看了更多的帖子后,我简化了我的问题 这是我的新控制器呼叫Javascript 茉莉花没有承诺,javascript,angularjs,jasmine,karma-jasmine,Javascript,Angularjs,Jasmine,Karma Jasmine,我不认识茉莉花。我试图让我的工厂承诺得到解决,但它似乎从来没有解决 这是我的工厂,它将收回一份公司名单 还有我的规格 正如您所看到的,我使用$rootScope.$digest来运行promise,但是我的期望从未达到,因此测试总是通过 我读过很多关于这方面的帖子,他们都说这应该行得通。我是做错了什么,还是错过了什么 编辑-简化方法 因此,在看了更多的帖子后,我简化了我的问题 这是我的新控制器呼叫 $scope.companies = []; var GetCompani
$scope.companies = [];
var GetCompanies = function(options) {
companyFactory.GetCompanies().then(function(response) {
options.success(response);
$scope.companies = response
});
}
$scope.GetCompanies = GetCompanies
我的工厂
function getCompanies() {
//call service and include company identifier in the service url
return $http.get(wunUtils.constants.serviceBaseUrl + 'CompanyService.svc/companies')
.then(function(response) {
return response.data;
}, function(err){
console.log(err);
});
}
var service = {
AddCompany: addCompany,
GetCompany: getCompany,
GetCompanies: getCompanies,
GetUserCompanyFleets: getUserCompanyFleets,
EditCompany: editCompany
};
还有我的新规格
describe('company', function() {
beforeEach(module('mockedCommercial'));
var $httpBackend, companyFactory, $rootScope, createController, scope
beforeEach(function() {
inject(function($controller, _$httpBackend_, _$rootScope_, _companyFactory_) {
$rootScope = _$rootScope_;
$httpBackend = _$httpBackend_;
scope = _$rootScope_.$new();
companyFactory = _companyFactory_;
createController = function() {
return $controller('ManageCompaniesController', {
'$scope': scope
});
};
});
})
it('should return available languages asynchronously', function(done) {
$httpBackend.whenGET(/https:\/\/myDomain.com\/V3_Fleet\/CompanyService.svc\/companies+/)
.respond(function(method, url, data, headers, params) {
return [{ 'id': 1 }];
}
});
//--------------------------
var dummy = [{ 'id': 1 }];
createController();
scope.GetCompanies()
$httpBackend.flush()
expect(scope.companies).toEqual(dummy)
scope.$digest(); done()
});
})) 在评估测试预期之前,请尝试使用
$httpBackend.flush()
这是angular建议的方法。有关更多详细信息和示例,您可以查看。在测试异步代码时,您有两个选项 1.对所有内容使用同步模拟 这包括:
- 模拟
$httpBackend
- 使用
$timeout.flush()
- 使用
$interval.flush()
- 使用
处理剩余承诺scope.$apply()
it('should...', function(done) {
$q.when().then(function() {
return $controller.GetSomething();
}).then(function() {
expect(scope.companies).toEqual(dummy);
}).then(done, done.fail);
});
因此,您可以在不完成的情况下编写
:
it('should...', function() {
return $q.when().then(function() {
return $controller.GetSomething();
}).then(function() {
expect(scope.companies).toEqual(dummy);
});
});
如果您有工具集,甚至可以使用async wait
:
it('should...', async () => {
await $controller.GetSomething();
expect(scope.companies).toEqual(dummy);
});
因此,经过大量阅读,我通过简化所有内容找到了答案。我认为我的问题来自于混合了$httpBackend.flush()和done()函数 似乎发生了这样的情况:done()函数触发它自己的摘要,因此当尝试使用flush()函数时,会出现冲突 这是我的工作工厂。它与我最初的purly有点不同,因为这是一个更简单的函数(出错的事情更少) 下面是我的简化规范$httpBackend.Flush() 编辑 还应注意的是,在测试HTTPBackend时,测试仅在使用expectGET和expectPOST而不是whenGET和whenPOST时有效
从我所看到的,whenGet用于模拟数据,而expectGet用于测试模拟您解决问题了吗?您的
公司工厂
中存在语法问题。您只是将getcompanys
声明为本地函数,而不是返回对象的方法。您是否需要从it
回调中返回承诺?请在$httpBackend.flush()之后查看我的editsTry调用范围。$apply()
)
要处理angular.hey@boyomarinov中排队的微任务,我尝试过这个方法,但仍然无法解决。请检查我的问题的编辑
it('should...', function() {
return $q.when().then(function() {
return $controller.GetSomething();
}).then(function() {
expect(scope.companies).toEqual(dummy);
});
});
it('should...', async () => {
await $controller.GetSomething();
expect(scope.companies).toEqual(dummy);
});
function getUser(userId) {
//call service and include user identifier in the service url
return $http.get(wunUtils.constants.serviceBaseUrl + 'myService.svc/fleetusers/' + userId)
.then(function(response) {
return response.data;
});
}
var service = {
GetMe: getMe,
GetUser: getUser,
AddUser: addUser,
EditUser: editUser,
ActivateUser: activateUser
};
describe('Testing httpBackend', function() {
var $httpBackend;
beforeEach(module('mockedCommercial'));
beforeEach(inject(function(_$httpBackend_) {
$httpBackend = _$httpBackend_;
}))
it('should behave...', function() {
inject(function(userFactory, $rootScope) {
var mockData = [];
$httpBackend.expectGET(wunUtils.constants.serviceBaseUrl + 'FleetUserService.svc/fleetusers/10').respond(mockData);
var promise = userFactory.GetUser(10);
$httpBackend.flush();
promise.then(function(result) {
expect(result).toEqual(mockData);
});
});
});
});