Jasmine 2.0 async done () and angular -mocks inject () in the same it () test

My usual test case looks like

it("should send get request", inject(function(someServices) { //some test })); 

And the Jasmine 2.0 asynchronous test should look like

 it("should send get request", function(done) { someAsync.then(function(){ done(); }); }); 

How can I use both made and inject in one test?

+42
javascript angularjs unit-testing mocking jasmine
Aug 12 '14 at 21:14
source share
4 answers

This should work; I encountered the same problem when upgrading to Jasmine 2.0

 it("should send get request", function(done) { inject(function(someServices) { //some async test done(); })(); // function returned by 'inject' has to be invoked }); 
+55
Dec 28 '14 at 1:31
source share

To add to the answer @Scott Boring and a comment by @WhiteAngel, which noted that the code inside the application was never called.

This worked for me:

 it("should send get request", function(done) { inject(function(someServices) { //some async test done(); })(); }); 
+10
Jul 06 '16 at 14:03
source share

IMPORTANT note is the brackets after the inject call. For example.

 inject(function(someServices) { //some async test done(); })(); <-- these brackets here important. 

If you look at the inject type:

 export declare function inject(tokens: any[], fn: Function): () => any; 

You can see that it returns a function, so you are not getting output because you forgot to call the function !!

If you think about it, it makes sense that it returns a function, because it performs a function!

Therefore, additional partners must solve the whole problem!

Working example:

  it('should allow you to observe for changes', function(done) { inject([GlobalStateService], (globalStateService: GlobalStateService) => { globalStateService.observe("user", storageType.InMemoryStorage, (user: string) => { expect(user).toBe("bla"); done(); }); globalStateService.write({ user: "bla"}, storageType.InMemoryStorage); })(); }); 
+8
Sep 20 '16 at 21:34
source share

You can write a test as follows:

 describe("Some service'", function () { var service; var data; beforeEach(function (done) { module('app'); inject(function (someService) { service = someService; }); service .getData() .then(function(result) { data = result; done(); }); }); it('should return a result', function () { expect(data).toBeDefined(); }); } 
+2
Aug 13 '14 at 10:25
source share



All Articles