I did a simple factory demo and I'm trying to test it using jasmine . I can run the test, but I use the spyOn method. I would prefer to use jasmine.createSpy or jasmine.createSpyObj to run the same test. Can someone help me reorganize my code to use these methods instead in my example?
http://plnkr.co/edit/zdfYdtWbnQz22nEbl6V8?p=preview
describe('value check',function(){ var $scope, ctrl, fac; beforeEach(function(){ module('app'); }); beforeEach(inject(function($rootScope,$controller,appfactory) { $scope = $rootScope.$new(); ctrl = $controller('cntrl', {$scope: $scope}); fac=appfactory; spyOn(fac, 'setValue'); fac.setValue('test abc'); })); it('test true value',function(){ expect(true).toBeTruthy() }) it('check message value',function(){ expect($scope.message).toEqual(fac.getValue()) }) it("tracks that the spy was called", function() { expect(fac.setValue).toHaveBeenCalled(); }); it("tracks all the arguments of its calls", function() { expect(fac.setValue).toHaveBeenCalledWith('test abc'); }); })
Update
angular.module('app',[]).factory('appfactory',function(){ var data; var obj={}; obj.getValue=getValue; obj.setValue=setValue; return obj; function getValue(){ return data; } function setValue(datavalue){ data=datavalue; } }).controller('cntrl',function($scope,appfactory){ appfactory.setValue('test abc'); $scope.message=appfactory.getValue() })
source share