0

I'm using Testem with Jasmine to set up an environment to start unit testing in my AngularJS app. Everything was working great until the first time I tried to use the injector. This is what I got back:

test.js

describe('Custom events', function(){
  beforeEach(module('AlchemyAdmin'));
  beforeEach(inject());

  it('should work', function() {
  });
});

Error console output:

Custom events should work.
    ✘ Error: [$injector:modulerr] http://errors.angularjs.org/1.2.25/$in
jector/modulerr?p0=AlchemyAdmin&p1=Error%3A%20%5B%24injector%3Amodulerr%
5D%20http%3A%2F%2Ferrors.angularjs.org%2F1.2.25%2F%24injector%2Fmodulerr
%3Fp0%3DdateRangePicker%26p1%3DError%253A%2520%255B%2524injector%253Amod
ulerr%255D%2520http%253A%252F%252Ferrors.angularjs.org%252F1.2.25%252F%2
524injector%252Fmodulerr%253Fp0%253Dpasvaz.bindonce%2526p1%253DError%252
53A%252520%25255B%252524injector%25253Anomod%25255D%252520http%25253A%25
252F%25252Ferrors.angularjs.org%25252F1.2.25%25252F%252524injector%25252
Fnomod%25253Fp0%25253Dpasvaz.bindonce%25250A%252520%252520%252520%252520
at%252520Error%252520(native)%25250A%252520%252520%252520%252520at%25252
0http%25253A%25252F%25252Flocalhost%25253A7357%25252Fvendor%25252Fangula
r%25252Fangular.min.js%25253A6%25253A450%25250A%252520%252520%252520%252
520at%252520http%25253A%25252F%25252Flocalhost%25253A7357%25252Fvendor%2
5252Fangular%25252Fangular.min.js%25253A20%25253A494%25250A%252520%25252
0%252520%252520at%252520http%25253A%25252F%25252Flocalhost%25253A7357%25
252Fvendor%25252Fangular%25252Fangular.min.js%25253A21%25253A502%25250A%
252520%252520%252520%252520at%252520http%25253A%25252F%25252Flocalhost%2
5253A7357%25252Fvendor%25252Fangular%25252Fangular.min.js%25253A33%25253
A267%25250A%252520%252520%252520%252520at%252520r%252520(http%25253A%252
52F%25252Flocalhost%25253A7357%25252Fvendor%25252Fangular%25252Fangular.
min.js%25253A7%25253A290)%25250A%252520%252520%252520%252520at%252520e%2
52520(http%25253A%25252F%25252Flocalhost%25253A7357%25252Fvendor%25252Fa
ngular%25252Fangular.min.js%25253A33%25253A207)%25250A%252520%252520%252
520%252520at%252520http%25253A%25252F%25252Flocalhost%25253A7357%25252Fv
endor%25252Fangular%25252Fangular.min.js%25253A33%25253A284%25250A%25252

Seems like there is something obvious I'm missing, but I can't quite grasp it. Note that taking out the line with the beforeEach(inject()); and writing standard tests in the it block works like a charm. Also, if I just declare an angular.module('myApp'); and then try to module() and inject() that, it works fine. Seems like something is going on in my module definition, maybe, but the app itself works fine with no errors from what I can tell!

Anybody run into this or know what I should look into? Thanks in advance!

Edit:

I thought it might make more sense if I gave a little context to my question. I have been developing an Angular app for a few weeks now, and I've been bit one to many times by not having unit tests. Having decided to TDD from here on out, I setup Testem, wrote a .spec.js file and tried to get started. I'm not testing any existing code, which will come later, but just trying to test-drive the part of the app I'm on. Before even writing my first piece of code or test, just setting up the module() and inject() calls per the docs failed miserably. That is where I am right now.

scott_trinh
  • 155
  • 1
  • 8
  • 1
    What do you expect by doing `beforeEach(inject())` ? – PSL Oct 01 '14 at 17:49
  • PSL: For right now, I just expect that it doesn't throw that error. Like I explained in my edit, if I make an "empty" module via `angular.module('myApp',[])` and then `module('myApp');inject()` I do not get an error. I'm trying to get to that place with the module I already have. – scott_trinh Oct 01 '14 at 18:45
  • But why cant you remove that line if you are not injecting anything at all? or do `beforeEach(inject(function(){}))` but it does not make sense to me.. – PSL Oct 01 '14 at 18:46
  • I plan on injecting lots of stuff, but it doesn't work. I went backwards until I just got to an empty injection call, and it still doesn't work, hence my posting here. Does that make sense? Sorry if I'm being confusing! – scott_trinh Oct 01 '14 at 18:51
  • Maybe it will help if I explain my situation a little bit more. I'll edit my question to give some context. – scott_trinh Oct 01 '14 at 18:54
  • To be more clear, changing `inject()` to `inject(function($rootScope){})` also does not work. – scott_trinh Oct 01 '14 at 19:06
  • That might be some missing stuff in your test. I have some jasmint/angular answers i wrote previously with _DEMO_. Here is one, http://stackoverflow.com/questions/25983466/angular-jasmine-testing-with-deffered-promises/25983973#25983973 . See if any of them provide you some direction. Check http://stackoverflow.com/search?tab=newest&q=user%3a1009603%20%5bjasmine%5d – PSL Oct 01 '14 at 19:23
  • I took a quick look at the answers you posted, thanks for that. None of them seem to quite address my situation that I cannot even call inject without the whole thing blowing up. To recap: I have a working AngularJS app. I have a single spec file that I described above as `test.js`, and it blows up. That makes me suspect something about my angular.module() declaration, but I can't imagine why it would work in the app, and not in the mock.module()... – scott_trinh Oct 01 '14 at 19:47
  • Prepare a plunker with the test that is fialing. There are many demos in my answers.. I am sure yours must be some silly mistake.. :) – PSL Oct 01 '14 at 19:59
  • http://plnkr.co/edit/ZElTe4naohdhYj2eBX4z?p=preview ... Man, that was painful... ;) – scott_trinh Oct 01 '14 at 20:21
  • 1
    alright.. Sorry i was away.. I told ya.. it might be a silly mistake.. :) – PSL Oct 01 '14 at 21:38

1 Answers1

0

Well, I shouldn't have gotten frustrated with the angular error links. If you keep clicking through them, I eventually found a sub-dependency that I was not linking to! If anyone else finds themselves in this particular pickle, I hope this helps them! I am closing the plunker I made to remove my live code from the public. Special thanks to PSL for responding so quickly and being so willing to try to understand my issue.

scott_trinh
  • 155
  • 1
  • 8