Jasmine 2.0 async done()和angular-mocks inject()在同一个testing中它()

我平时的testing用例看起来像

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

和Jasmine 2.0asynchronoustesting应该看起来像

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

我如何在一次testing中同时使用完成和注入?

这应该工作; 当我更新到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 }); 

要添加@Scott Boring的答案和@WhiteAngel的评论,他提到注入代码从来没有被调用过。

这对我工作:

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

重要提示是inject呼叫后的括号。 例如。

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

如果你看一下inject的types:

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

你可以看到它返回一个函数,所以你没有得到任何输出,因为你忘记了调用函数!

如果你仔细想想,它会返回一个函数,因为it需要一个函数!

所以额外的东西应该解决所有的问题!

工作示例:

  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); })(); }); 

你可以这样写testing:

 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(); }); }