Some questions about (unit) testing

746 views
Skip to first unread message

tst

unread,
Dec 5, 2012, 11:18:22 AM12/5/12
to ang...@googlegroups.com
I have couple questions:
1. Looking here, here and some other places I see that there are underscore symbols around some parameters (like _$httpBackend_). Is that some naming conventions or something else?
2. In my app.js file I have these:
        router.when('/campaigns', {
            templateUrl: '/partials/items.html',
            controller: 'ItemListController',
            resolve: resolve
        });
resolve contains a resolveFunction returning promise. There is an assignment inside these function:

$rootScope.accountInfo = data;
 
 I am trying to write unit test for ItemListController. When I run Testacular:

info: Testacular server started at http://localhost:9876/
info (launcher): Starting browser Chrome
info (Chrome 25.0): Connected on socket id Z6A2vK9HrZ3LB3go0Z7N
Chrome 25.0 application controllers ItemListController bar === bar FAILED
TypeError: Cannot read property 'created_at' of undefined
   at new ItemListController (/home/www/myapp/src/controllers/ItemListController.js:19:120)
   at invoke (/home/www/myapp/contrib/angular/angular.js:2809:28)
   at Object.instantiate (/home/www/myapp/contrib/angular/angular.js:2819:23)
   at /home/www/myapp/contrib/angular/angular.js:4639:24
   at null.<anonymous> (/home/www/myapp/test/unit/controllerSpec.js:8:20)
   at Object.invoke (/home/www/myapp/contrib/angular/angular.js:2809:28)
   at workFn (/home/www/myapp/test/lib/angular/angular-mocks.js:1731:20)
Error: Declaration Location
   at window.jasmine.window.inject.angular.mock.inject (/home/www/myapp/test/lib/angular/angular-mocks.js:1717:25)
   at null.<anonymous> (/home/www/myapp/test/unit/controllerSpec.js:6:20)
   at null.<anonymous> (/home/www/myapp/test/unit/controllerSpec.js:5:5)
   at /home/www/myapp/test/unit/controllerSpec.js:1:1
Chrome 25.0: Executed 1 of 1 (1 FAILED)
e 25.0: Executed 1 of 1 (1 FAILED) (0.173 secs / 0.034 secs)
 
First error caused by calling $rootScope.accountInfo.created_at inside my $rootScope.accountInfo.created_at becaused in is not recieved by resolveFunction described above.
So how can I mock these inside controller test?
What does second error mean?

My test spec below:
describe('application controllers', function () {
    var scope;
    var ctrl;
    beforeEach(module('application'));
    describe('ItemListController', function () {
        beforeEach(inject(function($rootScope, $controller) {
            scope = $rootScope.$new();
            ctrl = $controller('ItemListController', {$scope: scope});
        }));
        it('bar === bar', function () {
            expect('bar').toBe('bar');
        });
    });
});
3. It's too hard to learn unit testing reading the docs on angular website. Is there any tutorials elsewhere? The most difficult parts are mocking promises and injecting.

Peter Bacon Darwin

unread,
Dec 5, 2012, 11:53:48 AM12/5/12
to ang...@googlegroups.com
1. The _underscores_ wrapping a parameter are ignored by the injectors.  This is simply syntactic sugar to allow you to assign to a variable that is scoped outside the function that you would like to have with the same name as the service:

var myService;
function(_myService_) {
  myService = _myService_;
}
myService.doStuff();

2. In your test you need to pass a value in to the $controller call along with the $scope for the value of the resolve.  This doesn't need to be a promise.  The route service will wait for the promise to resolve and then inject the resolved value into the controller.

ctrl = $controller('itemListController', { $scope: scope, resolveName: { some: data }});

3. Pawel and my book will have a "really" good section on unit testing.  Out next year - but there may be some early access to chapters available soon!


--
You received this message because you are subscribed to the Google Groups "AngularJS" group.
To post to this group, send email to ang...@googlegroups.com.
To unsubscribe from this group, send email to angular+u...@googlegroups.com.
Visit this group at http://groups.google.com/group/angular?hl=en-US.
 
 

Michael Bielski

unread,
Feb 26, 2013, 11:50:07 AM2/26/13
to ang...@googlegroups.com
Any progress to report on that book, Peter? I could really use some good info on unit testing right about now!

Peter Bacon Darwin

unread,
Feb 27, 2013, 4:34:23 PM2/27/13
to ang...@googlegroups.com
Not finished yet.  Sorry!


On 26 February 2013 16:50, Michael Bielski <michael...@yahoo.com> wrote:
Any progress to report on that book, Peter? I could really use some good info on unit testing right about now!

--
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 angular+u...@googlegroups.com.

To post to this group, send email to ang...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Michael Bielski

unread,
Feb 27, 2013, 5:39:42 PM2/27/13
to ang...@googlegroups.com
Aww man... you're killing me! 

Please do keep us posted on when we can expect a release date. Oh, and please convince your publisher to release it as an e-book, whether it is Kindle, ePub, or PDF format.

Witold Szczerba

unread,
Feb 27, 2013, 7:00:12 PM2/27/13
to ang...@googlegroups.com
Do you have some particular issue? I am asking, because in my project, we have many js tests of directives and unless the directive is doing some extraordinary magic with DOM (or when it does not interact heavily with controllers/services) there are no problems.

Regards,
Witold Szczerba


On 27 February 2013 23:39, Michael Bielski <michael...@yahoo.com> wrote:
Aww man... you're killing me! 

Please do keep us posted on when we can expect a release date. Oh, and please convince your publisher to release it as an e-book, whether it is Kindle, ePub, or PDF format.

--

Michael Bielski

unread,
Feb 27, 2013, 7:16:04 PM2/27/13
to ang...@googlegroups.com
The only issue I have is going through the pains of learning testing in general in addition to testing Angular. While the amount of information about Angular in general that is available has greatly improved in the 7 months that I've been trying to use it (with varying levels of success,) the amount of information about testing Angular apps is still lagging behind. If I understood what I was doing well enough I'd certainly write something up about it and share it around, but I'm afraid that isn't the case. Peter specifically mentioned that his upcoming book (that he is co-authoring with Pawel) will be full of good testing information, and I'm really looking forward to it. Thank you very much for asking, though. I greatly appreciate the help I get from the community and I try to give back whenever I can.
Reply all
Reply to author
Forward
0 new messages