javascript - Now I have error "ReferenceError: items is not defined" and cannot ideas how I can test my dataService. -
i need help, added jasmine tast factory. code is...
---dataservice.js---
angular.module('angularappapp') .factory('dataservice', function($resource){ homecoming $resource(`http://...:3100/posts/:id`, null, { 'update': { method:'put' } }); })
---onepostctrl.js ---
angular.module('angularappapp') .controller('onepostctrl', ['$scope', '$http', '$routeparams', 'dataservice', function ($scope, $http, $routeparams, dataservice) { dataservice.get ({id: $routeparams.postid}).$promise.then(function(data){ $scope.postinfo = data; }); }]);
-- main container ---
angular.module('angularappapp').controller('postctrl', ['$scope','$http', 'ngdialog', 'dataservice','trimservice', function ($scope, $http, ngdialog, dataservice, trimservice) { //save info remote server loaded pop-up $scope.savepost = function(){ $scope.addformdata.date = $scope.formated_date; dataservice.save($scope.addformdata, function() { laoddata(); }); ngdialog.closeall(); }; //delete post remote server $scope.deletepost = function(article) { dataservice.delete({ id: article._id }, function() { laoddata(); }); }; //edit post remote server $scope.updatepost = function (article) { dataservice.update({ id: article._id},article).$promise.then(function() { laoddata(); }); ngdialog.closeall(); }
}]);
--- mock info ---
angular.module('mock', []).value('items', [{ ... }]
---at index.html have loaded mocks scripts---
src="bower_components/angular-mocks/angular-mocks.js" src="mosk_data/mocks.module.js"
--jasmine tests ...
describe("factory of dataservice", function (){ var $httpbackend, $http, $q, factory; beforeeach(module("angularappapp")); beforeeach(module('mock')); beforeeach(function(){ inject(function($injector, _$httpbackend_,_$http_,_$q_){ $q = _$q_; $http = _$http_; $httpbackend = _$httpbackend_; $httpbackend.when('get', '/items').respond(items); mill = $injector.get('dataservice'); }); aftereach(function () { $httpbackend.verifynooutstandingexpectation(); $httpbackend.verifynooutstandingrequest(); }); it("data service", function(){ });
});
now, have error "referenceerror: items not defined" , cannot ideas how can test dataservice.
you forgot inject value , assign variable in tests. seek this:
var $httpbackend, $http, $q, factory, items; //declare variable items here (or can within beforeeach) beforeeach(module("angularappapp")); beforeeach(module('mock')); beforeeach(function(){ inject(function($injector, _$httpbackend_,_$http_,_$q_, _items_){ $q = _$q_; $http = _$http_; //inject value , assign variable items = _items_ $httpbackend = _$httpbackend_; $httpbackend.when('get', '/items').respond(items); mill = $injector.get('dataservice'); });
the reference error
got because there no variable called items
. defined angular value name items
, it's not same variable - think of lives "somewhere within angular guts" , utilize have inject , utilize normal variable.
javascript angularjs mocking jasmine karma-jasmine
No comments:
Post a Comment