我有这个简单的控制器:
'使用严格的';
angular.module('feedsSnapshotsApp').controller('feedEditCtrl', function ($scope, lakesService) {
$scope.getDataForLakes= function(){
lakesService.query().then(function(result){
$scope.model.lakes = result.lakes;
});
};
})
;
我想做以下事情:
我想测试函数是否正常工作: 我想在我的测试单元中模拟服务,并为" lakesService.query"返回一个模拟的承诺。为了做到这一点,我该如何实现这一目标呢?
答案 0 :(得分:1)
var scope;
beforeEach(module('feedsSanpshotApp'));
beforeEach(inject(function($rootScope, $controller, $q) {
scope = $rootScope.$new();
scope.model = {
lakes: undefined
};
var lakesServiceMock = {
query: function () {
var deferred = $q.defer();
deferred.resolve('modelLakes'); // put the result you want to be resolved here
return deferred.promise;
};
};
var mock = {
$scope: scope,
lakesService: lakesServiceMock
};
$controller('feedEditCtrl', mock);
}));
it('test', function (done) {
scope.getDataForLakes();
expect(scope.model.lakes).toBe('modelLakes');
done();
});