[英]AngularJS Testing with Jasmine and Karma
i try to test an angular controller but he returns a promise and i cant really resolve it...what's wrong with my code? 我尝试测试角度控制器,但他返回了一个Promise,但我无法真正解决它……我的代码有什么问题?
the Angular Controller: Angular控制器:
kpi.AboutController = function ($scope, Version) {
function loadVersion() {
//$scope.version = Version.getVersion();
$scope.version = '1.1.0';
}
loadVersion();
};
and the jasmine test: 和茉莉花测试:
describe('dashboard: AboutController', function() {
beforeEach(module('dashboard'));
var $controller;
beforeEach(inject(function(_$controller_){
// The injector unwraps the underscores (_) from around the parameter names when matching
$controller = _$controller_;
}));
it('testing the version number', function() {
var $scope = {};
var controller = $controller('AboutController', { $scope: $scope });
var version = 0;
console.log($scope.version);
expect($scope.version).toContain("1.");
});
});
this is working, but when i change the line $scope.version = '1.1.0';
这是$scope.version = '1.1.0';
,但是当我更改$scope.version = '1.1.0';
to $scope.version = Version.getVersion();
到$scope.version = Version.getVersion();
i receive a promise and i can not rly check it....i tried to resolve it with "then()" function via $scope.version.then(...)
but that did not work...what to do? 我收到一个承诺,我不能RLY检查....我试图用“那么()”通过功能来解决它$scope.version.then(...)
但没有工作...怎么办?
edit: 编辑:
the following error occures: 发生以下错误:
Expected e({ $promise: Object({ $$state: Object({ status: 0 }) }), $resolved: false }) to contain '1.'.
at Object.<anonymous>
and the Service: 和服务:
kpi.VersionFactory = function ($resource, appConfig) {
var Version = $resource(thePath, {}, {
"getVersion": {
method: "GET",
url: thePath,
isArray: false
}
});
return Version;
};
you need to pass a callback into your test case . 您需要将回调传递到测试用例中。
kpi.AboutKPIController = function ($scope, Version) {
$scope.loadVersion= function () {
Version.getVersion().then(function(res){
$scope.version = res.data;
})
}
$scope.loadVersion();
};
describe('dashboard: AboutKPIController', function() {
beforeEach(module('dashboard'));
var $controller;
beforeEach(inject(function(_$controller_){
// The injector unwraps the underscores (_) from around the parameter names when matching
$controller = _$controller_;
}));
it('testing the version number', function(done) {
var $scope = {};
var controller = $controller('AboutKPIController', { $scope: $scope });
var version = 0;
console.log($scope.version);
$scope.loadVersion().then(function(res)){
//can test here
expect($scope.version).toContain("1.");
done();
});
});
}); });
I expect Version.getVersion();
我期望Version.getVersion();
returns a promise. 返回承诺。 Your controller implemnentation should look like 您的控制器实现应如下所示
function loadVersion() {
Version.getVersion().then(function(version) {
$scope.version = version;
});
}
In your test code use $scope.$apply
to resolve promises before you perform except
. 在您的测试代码中使用$scope.$apply
执行之前解决承诺except
。
beforeEach(inject(function(_$controller_, _$rootScope_){
$controller = _$controller_;
$rootScope = _$rootScope_;
}));
it('testing the version number', function() {
var $scope = $rootScope.$new();
var controller = $controller('AboutKPIController', { $scope: $scope });
controller.loadVersion();
$scope.$apply();
expect($scope.version).toContain("1.");
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.