jeudi 5 février 2015

Angular Mocking Testing Factory Object

Relevant Code: http://ift.tt/1v2BQCg


I have a factory which is instantiated in a controller on the scope in the following manner. This object is extremely simple but does a few things.


$scope.selector = new SelectorFactory('type1') <-- 'the following manner'



  1. given a strategy it selects the proper api endpoints to use for the correct data

  2. appends meta object to the objects within the array returned by the api/strategy used.

  3. the api response data is an array of similar objects.


I have a couple of questions the first is regarding the manner in which i implemented this particular pattern and the next (more important one) is how do I go about testing this; I am familiar with jasmine matchers and what not but I am entirely stuck on mocks/spys/stubs and angular's test injection process. I have researched for a couple of hours and can't seem to figure this out and am getting sick of the assortment of alternative and contradictory ways of doing this.


Now to the questions: Question 1


Given the SelectorFactory code and how it is ultimately used in its current ($scope.selector = new SelectorFactory('type1')) manner. Should this be changed to a method which is instantiated more along the lines of:


$scope.selector = SelectorFactory.newSelector('type');


I see a few benefits to this method:



  • Verbosity; we don't want a new SelectorFactory we only want a SelectorObject, which is a product of the factory. This makes me feel as if I am doing something slightly wrong in how I am implementing this.

  • I feel, for some reason, this alternative method might be easier to test, is this feeling correct?


Question 2


Forgive the lack of testing code. I just cleared it out as it was not working and I was using info from multiple sources and getting very strange errors. How would I go about testing this using built in jasmine methods such as spy or provide? I essentially just want to create a test that checks the metadata is correctly appended given the strategy used.


Aucun commentaire:

Enregistrer un commentaire