angularjs - what and how to do unit test for angular factories -


angular factory great!! while writing unit test, kind of confusing, should write unit test or not.

i have following factory:

(function(myapp) { myapp.factory('myfirstfactory', function(mysecondfactory){   function myfirstfactory(config){     this.value1 = 'value1';     setdefault(this);   }     myfirstfactory.dosomething = function(){      var config = {        findwork: true;        mywork: mysecondfactory.dowork()      };      return new myfirstfactory(config);    };    return myfirstfactory; }); })(angular.module('mymodule')); 

so above factory, need write unit test myfirstfacotry , dosomething function? if yes, how can achieve using jasmine , karma.. tried following:

describe('myfirstfactory', function() {    var mockmyfirstfactory;    beforeeach(function(){      module('mymodule');      inject(function(myfirstfactory){         mockmyfirstfactory = myfirstfactory;      });    });    it('myfirstfactory should defined', function(){      expect(mockmyfirstfactory).tobedefined();   });   it('should something', function(){     // how test dosomething    }); }); 

i using jasmine, angular-mock, karma.

like unit testing in general, scrutinizing behavior of code through tests. if have expectation of have happened after call dosomething(), should have test reflect expectation.

  it('should something', function(){     mockmyfirstfactory.dosomething(thing);     expect(mockmyfirstfactory.somethingbool).tobe(true);     expect(mockmyfirstfactory.somethingdone).tobe("some value");     expect(mockmyfirstfactory.somemethod).tohavebeencalled();   }); 

here snippet of sample of 1 of our factories have built tests for simple general setup , test behavior.

    describe('imageuploader.imagemodel module', function () {         // variables         var imagemodel, imagemodelfactory, $rootscope, shareddataservice;         // inject , setup dependencies         beforeeach(module('imagemodel'));          beforeeach(inject(function (_$rootscope_, $injector, _imagemodelfactory_) {             $rootscope = _$rootscope_;             shareddataservice = $injector.get('shareddataservice');             imagemodelfactory = _imagemodelfactory_;             spyon($rootscope, '$apply');         }));         // set mock data         beforeeach(function () {             shareddataservice.metadata = [{metadataitemone: "one", metadataitemtwo: "two"}];             var loadeddata = {                 name: 'test_name.jpg',                 target: {                     result: 'fakedata12345'                 }             };             var fake_file = {                 data: "fakedata12345",                 filename: loadeddata.name,                 filetype: "image",                 filesize: 1024,                 filedate: "7-7-2015"             };             imagemodel = imagemodelfactory(fake_file, loadeddata);         });         // test behavior         it('can instance of imagemodelfactory', function () {             expect(imagemodelfactory).tobedefined();         });         it('can instantiate instance of imagemodel', function () {             expect(imagemodel).tobedefined();         });         it('can set upload state', function () {             imagemodel.setuploaded(true);             expect(imagemodel.isuploaded).tobe(true);             expect(shareddataservice.imagessucceeded).tobe(1);             imagemodel.setuploaded(false);             expect(imagemodel.isuploaded).tobe(false);             expect(shareddataservice.imagessucceeded).tobe(0);             expect(imagemodel.relatedtoupload).tohavebeencalled()         });     } 

if trying figure out how jasmine, there plenty of documentation , examples started, right here jasmine spies


Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -