This tutorial showcases my approach to mocking resource objects in an Angular controller using SinonJs for faking a resource object and $q for simulating promise chains.
To mock the promise chain, you must obtain a defer object from $q and then extract a promise from it.
During testing, you can mimic either a successful or failed outcome by utilizing promise.resolve()
or promise.reject()
. Passing an object as a parameter allows you to simulate server data like so: promise.reject(someData)
.
Remember to execute scope.apply()
to ensure that your intended changes are reflected on the scope.
While I'm not completely certain if this methodology is ideal, it has proven effective in my experience.