我试图通过以下方式进行单元测试:
https://developers.livechatinc.com/blog/testing-angular-2-apps-dependency-injection-and-components/
位修改:
it('should render on id testRenamed', injectAsync(
[TestComponentBuilder], (tcb: TestComponentBuilder) => {
return tcb.createAsync(TodoItemRenderer)
.then((componentFixture: ComponentFixture) => {
const element = componentFixture.nativeElement;
componentFixture.componentInstance.testRenamed = 'John';
componentFixture.detectChanges();
expect(element.getElementById("testRenamed").innerHTML).toEqual('John');
})
}));
并收到错误 - 请参阅标题。
还尝试从此复制测试:
https://stackoverflow.com/questions/34620977/angular2-injectasync-testing
it('should be able to get', injectAsync([Http], (http: Http) => {
return http.get('http://www.google.com')
.toPromise()
.then((res: Response) => {
expect(false).toBeTruthy();
console.log('RES', res.json());
}, (err) => {
expect(false).toBeTruthy();
console.log('ERR', err);
})
.catch((err) => {
expect(false).toBeTruthy();
console.log('CATCH', err);
})
}));
因为他没有写出他得到的错误,他有不同的问题。我仍然得到同样的错误。
当我用鼠标悬停时,我在webstorm中看到此错误。我的测试也是红色的。
TodoItemRenderer should render on id testRenamed
TypeError: queueableFn.fn.call is not a function
TypeError: queueableFn.fn.call is not a function
at attemptSync (http://127.0.0.1:8080/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:1886:24)
at QueueRunner.run (http://127.0.0.1:8080/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:1874:9)
at QueueRunner.execute (http://127.0.0.1:8080/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:1859:10)
at Spec.Env.queueRunnerFactory (http://127.0.0.1:8080/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:697:35)
at Spec.execute (http://127.0.0.1:8080/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:359:10)
at Object.fn (http://127.0.0.1:8080/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:2479:37)
at attemptAsync (http://127.0.0.1:8080/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:1916:24)
at QueueRunner.run (http://127.0.0.1:8080/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:1871:9)
at QueueRunner.execute (http://127.0.0.1:8080/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:1859:10)
at Env.queueRunnerFactory (http://127.0.0.1:8080/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:697:35)
当我点击这个功能定义时,我看到:
/**
* Allows injecting dependencies in `beforeEach()` and `it()`. The test must return
* a promise which will resolve when all asynchronous activity is complete.
*
* Example:
*
* ```
* it('...', injectAsync([AClass], (object) => {
* return object.doSomething().then(() => {
* expect(...);
* });
* })
* ```
*
* @param {Array} tokens
* @param {Function} fn
* @return {FunctionWithParamTokens}
*/
export declare function injectAsync(tokens: any[], fn: Function): FunctionWithParamTokens;
这里有什么问题?