Question

I am writing jasmine test case for state in angular JS.

My state has resolve section something like this.

resolve: {
             myResult: function () {
                 var dfd = $q.defer();
                 var queryOptions = new PreparedQueryOptions().$expand(["children"]).effectivity(myPredicate);
                 //Rest of the code
                 return dfd.promise;
             }
         }

Here I extended PreparedQueryOptions to add effectivity method in it. Now from my test case when I made following call

myState.definition.resolve.myResult();

Its throwing error as "effectivity is not a function". But $expand is getting executed properly.

PreparedQueryOptions don't have this method, but I added this and its working properly (without any errors).

How to solve thise problem? Should I write Spy on these method and how?

Était-ce utile?

La solution

The code you are testing calls an extension you wrote for PreparedQueryOptions. If you haven't included that extension to your testing framework, then your test will fail since that extension will be called and will be undefined.

Since you aren't testing either PreparedQueryOptions or the extension, I would suggest stubbing them out for your test cases. If you are using Jasmine, you could do something like this:

var PreparedQueryOptions = jasmine.createSpy('PreparedQueryOptions');
PreparedQueryOptions.$extend = jasmine.createSpy('$extend');

//return empty object so that queryOptions variable will be defined
PreparedQueryOptions.$extend.effectivity = function() { return {}};
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top