Tests for app "run" block

141 views
Skip to first unread message

Sergio Rinaudo

unread,
Dec 22, 2013, 7:27:53 PM12/22/13
to ang...@googlegroups.com
Hi all,
I have some logic in the run block of my app

file app.js
app.config(
     // routes etc..
)..run(
    // logic here
);

this logic have to run only once when the user enters the app,
for example an async request to get the user session data.

I would like to set up tests for this block, but after lots of searching I
couldn't find any way to do so.

I would like to follow all the best practices, so, the first question is,
is the run block the correct point to do this kind of checking?
If not, what would be a better solution?
Please note that I did not use the resolve property because the application needs this checking
in every module but just once (when the use enter th app, that could be any controller).

I hope that someone could enlight me!

Regards


Clint Checketts

unread,
Dec 23, 2013, 5:44:12 PM12/23/13
to ang...@googlegroups.com
If you had an InitializationService that did all this work, then it would easy to test like any other service, and the run block would be simplified to merely calling the Init service.


--
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.
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/groups/opt_out.

Sergio Rinaudo

unread,
Dec 23, 2013, 5:56:40 PM12/23/13
to ang...@googlegroups.com
Could you provide a simple example?

Currently, I have something like this

- A LoggedInLoader that is a Service that returns a promise.
- This service is called in the run block


If a try to run tests I get this error

Error: Unexpected request: GET /url/to/service
No more request expected in app/bower_components/angular-mocks/angular-mocks.js (line 960)

I am really stuck here...




2013/12/23 Clint Checketts <chec...@gmail.com>

--
You received this message because you are subscribed to a topic in the Google Groups "AngularJS" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/angular/QqrpFe8kSr0/unsubscribe.
To unsubscribe from this group and all its topics, send an email to angular+u...@googlegroups.com.

Clint Checketts

unread,
Dec 24, 2013, 12:01:10 AM12/24/13
to ang...@googlegroups.com
Sounds like you are struggling with $httpBackend. 

Do you have a line like this?
  1. $httpBackend.when('GET', '/url/to/service').respond({userId: 'userX'}, {'A-Token': 'xxx'});
  2.  





Sergio Rinaudo

unread,
Dec 24, 2013, 1:48:36 PM12/24/13
to ang...@googlegroups.com
Yes, this is exactly what I have inside the test.


2013/12/24 Clint Checketts <chec...@gmail.com>
Reply all
Reply to author
Forward
0 new messages