javascript Jasmine 2.0 async done() 和 angular-mocks inject() 在同一个测试 it()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25274152/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Jasmine 2.0 async done() and angular-mocks inject() in same test it()
提问by huston007
My usual test case looks like
我通常的测试用例看起来像
it("should send get request", inject(function(someServices) {
//some test
}));
And Jasmine 2.0 async test should look like
Jasmine 2.0 异步测试应该看起来像
it("should send get request", function(done) {
someAsync.then(function(){
done();
});
});
How can I use both done and inject in one test?
如何在一次测试中同时使用完成和注入?
回答by Scott Boring
This should work; I ran into the same problem when I updated to Jasmine 2.0
这应该有效;当我更新到 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
});
回答by Arie Milner
An IMPORTANT note is the brackets after the inject
call. Eg.
一个重要的注意事项是inject
调用后的括号。例如。
inject(function(someServices) {
//some async test
done();
})(); <-- these brackets here important.
If you look at the type of inject
:
如果你看一下类型inject
:
export declare function inject(tokens: any[], fn: Function): () => any;
You can see it returns a function, so you were getting no output because you forgot to call the function!!
您可以看到它返回一个函数,因此您没有得到任何输出,因为您忘记调用该函数!!
If you think about it, it makes sense that it returns a function, because it
takes a function!
如果你仔细想想,它返回一个函数是有道理的,因为it
它接受了一个函数!
So the extra parentheses should solve all 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);
})();
});
回答by Pierre-Luc Gregtheitroade
To add to the answer of @Scott Boring and to the comment of @WhiteAngel who mentionned that the code inside inject was never called.
添加到@Scott Boring 的答案和@WhiteAngel 的评论中,他提到了注入中的代码从未被调用过。
This worked for me:
这对我有用:
it("should send get request", function(done) {
inject(function(someServices) {
//some async test
done();
})();
});
回答by David Bohunek
You could write the test like that:
你可以这样写测试:
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();
});
}
回答by frot.io
For Angular 5.2.0: @scott-boring's approach did not work for me. What did work is using the TestBed.get()
to get the services instead of inject()
, as described in the docs:
对于 Angular 5.2.0:@scott-boring 的方法对我不起作用。工作是使用TestBed.get()
来获取服务而不是inject()
,如文档中所述:
describe('TooltipComponent', () => {
let component: TooltipComponent;
let fixture: ComponentFixture<TooltipComponent>;
let myService: MyService;
beforeEach(async(() => {
const myServiceSpy = jasmine.createSpyObj('MyService', ['calc']);
TestBed.configureTestingModule({
declarations: [ MyComponent ],
providers: [
{provide: MyService, useValue: myServiceSpy}
]
})
.compileComponents();
myService = TestBed.get(MyService);
}));
beforeEach(() => {
fixture = TestBed.createComponent(MyComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should render correctly', (done) => {
component.render();
setTimeout(() => {
expect(myService.calc).toHaveBeenCalled();
done();
}, 1000);
});