angularjs - How to test an Angular Factory which uses Parse SDK -



angularjs - How to test an Angular Factory which uses Parse SDK -

this first question here. i'll seek best. searched lot before posting

i'm developing angularjs application relying on javascript parsesdk. have convinced myself dive in testing recently, beginner.

i have mill userfactory wraps around sdk clean , modular angular way. ie: sdk used through factories (not controller nor directives).

it goes this:

mymodule.factory('userfactory', ['$q', function($q){ var user = parse.user.extend({ // instance methods },{ // static/class methods // overrides parse.user.current() wrap current user in promise current: function(){ var deferred = $q.defer(); var currentuser = parse.user.current(); if(currentuser) deferred.resolve(currentuser); else deferred.reject("no current user"); homecoming deferred.promise; } }); homecoming user; }]);

my question: how test userfactory.current() knowing uses external service? i've looked mocking parse sdk don't know how since it's not angular related (ie: can't utilize httpbackend).

my current test file:

describe("unit: userfactory", function(){ var userfactory; beforeeach(function(){ module("mymodule"); inject(function(_userfactory_){ userfactory = _userfactory_; }); }); it("should homecoming current user", function(){ // expect ? }); });

thank in advance

describe("unit: userfactory", function(){ var userfactory; beforeeach(function(){ module("mymodule"); inject(function(_userfactory_){ userfactory = _userfactory_; $rootscope = _$rootscope_; }); }); describe('current()', function() { var successcallback, errorcallback; beforeeach(function() { successcallback = jasmine.createspy('success'); errorcallback = jasmine.createspy('error'); }); it("promise should resolve if parse.user.current truthy", function(){ spyon(parse.user, 'current').and.returnvalue(true); userfactory.current().then(successcallback, errorcallback); $rootscope.$digest(); expect(successcallback.calls.count()).tobe(1); expect(errorcallback.calls.count()).tobe(0); expect(parse.user.current).tohavecalledonce(); }); it("promise should reject if parse.user.current falsy", function(){ spyon(parse.user, 'current').and.returnvalue(false); userfactory.current().then(successcallback, errorcallback); $rootscope.$digest(); expect(errorcallback.calls.count()).tobe(1); expect(successcallback.calls.count()).tobe(0); expect(parse.user.current).tohavecalledonce(); }); }); });

angularjs unit-testing parse.com jasmine karma-runner

Comments

Popular posts from this blog

formatting - SAS SQL Datepart function returning odd values -

c++ - Apple Mach-O Linker Error(Duplicate Symbols For Architecture armv7) -

php - Yii 2: Unable to find a class into the extension 'yii2-admin' -