You make a good point. And in truth, my primary interest is not in
verifying what's injected. I'm exploring the creation of a jasmine
extension that will greatly assist in the creation of mocks for all
dependencies injected into controllers, services, filters, etc.
The concept is this:
* mock all dependencies,
* in your "given" statements, specify how these mocks will respond when
called,
* only in your "then" statements do you passThrough to the actual code.
This gives the code under test total isolation, and forces your tests to be
explicit about the conditions for the test. If you follow the AngularJS
mandate that dependencies be injected, and you have access to those
dependencies in the setup for the test, then you should be able to mock all
dependencies with a single line of code. Something like:
dep = spyOnControllerDependencies(MyController);
Where "dep" is an object containing the mocked/spied on dependencies.
Perhaps I should have been more forthcoming at the get go, but I was
worried about verging into tl;dr territory.
-michael
On Monday, April 14, 2014 8:56:10 AM UTC-7, Witold Szczerba wrote:
>
> Hi,
> why would you need this? Testing if something was injected into
> controller sounds like an anti-pattern. Who cares how was the
> controller created if it can (green light) or cannot (red light) do
> it's job?
>
> Regards,
> Witold Szczerba
>
> 2014-04-13 22:42 GMT+02:00 Michael Kelly <[email protected]<javascript:>>:
>
> > I'd like to be able to verify what's being injected into my controller,
> and
> > further, to use that information in additional tests.
> >
> > I'd expect to be able to do something like this:
> >
> > var app = angular.module('MyApp',[]);
> > app.controller('MyController', ['$scope', '$q', function($scope, $q)
> {
> > $scope.myFn = function () {};
> > }]);
> >
> > describe('MyController', function () {
> >
> > it('should require $scope and $q', function () {
> >
> expect(app.controller("MyController").requires).toBe(['$scope',
> > '$q']);
> > });
> >
> > });
> >
> > But when I execute this test I get:
> >
> > Expected [ ] to be [ '$scope', '$q' ]
> >
> > Any help would be appreciated,
> >
> > -michael
> >
> > --
> > You received this message because you are subscribed to the Google
> Groups
> > "AngularJS" group.
> > To unsubscribe from this group and stop receiving emails from it, send
> an
> > email to [email protected] <javascript:>.
> > To post to this group, send email to [email protected]<javascript:>.
>
> > Visit this group at http://groups.google.com/group/angular.
> > For more options, visit https://groups.google.com/d/optout.
>
--
You received this message because you are subscribed to the Google Groups
"AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.