typescript 错误:StaticInjectorError(DynamicTestModule)[NotificationsComponent
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/50449449/
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
Error: StaticInjectorError(DynamicTestModule)[NotificationsComponent
提问by Sangwin Gawande
Full Error when run ng test
:
运行时完全错误ng test
:
Error: StaticInjectorError(DynamicTestModule)[NotificationsComponent
AuthService]:? ? StaticInjectorError(Platform: core)[NotificationsComponent AuthService]:?
?NullInjectorError: No provider for AuthService!
Expected undefined to be truthy.
Error: Expected undefined to be truthy.
? ? at stack (http://localhost:9876/base/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:2176:17)
? ? at buildExpectationResult (http://localhost:9876/base/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:2146:14)
? ? at Spec.expectationResultFactory (http://localhost:9876/base/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:766:18)
? ? at Spec.addExpectationResult (http://localhost:9876/base/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:444:34)
? ? at Expectation.addExpectationResult (http://localhost:9876/base/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:710:21)
? ? at Expectation.toBeTruthy (http://localhost:9876/base/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:2099:12)
? ? at Object. (http://localhost:9876/_karma_webpack_/webpack:/C:/../webTest/src/app/components/notifications/notifications.component.spec.ts:97:23)
? ? at ZoneDelegate.webpackJsonp.../../../../zone.js/dist/zone.js.ZoneDelegate.invoke (http://localhost:9876/_karma_webpack_/webpack:/C:/C:/../webTest/node_modules/zone.js/dist/zone.js:388:1)
? ? at ProxyZoneSpec.webpackJsonp.../../../../zone.js/dist/proxy.js.ProxyZoneSpec.onInvoke (http://localhost:9876/_karma_webpack_/webpack:/C:/../webTest/node_modules/zone.js/dist/proxy.js:79:1)
? ? at ZoneDelegate.webpackJsonp.../../../../zone.js/dist/zone.js.ZoneDelegate.invoke (http://localhost:9876/_karma_webpack_/webpack:/C:/../webTest/node_modules/zone.js/dist/zone.js:387:1)
错误:StaticInjectorError(DynamicTestModule)[NotificationsComponent
AuthService]:?? StaticInjectorError(平台:核心)[NotificationsComponent AuthService]:?
?NullInjectorError: 没有 AuthService 的提供者!
预期 undefined 为真。
错误:预期未定义为真。
? ? 在堆栈(http://localhost:9876/base/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:2176:17)
? ? 在 buildExpectationResult ( http://localhost:9876/base/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:2146:14)
? ? 在 Spec.expectationResultFactory ( http://localhost:9876/base/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:766:18)
? ? 在 Spec.addExpectationResult ( http://localhost:9876/base/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:444:34)
? ? 在 Expectation.addExpectationResult ( http://localhost:9876/base/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:710:21)
? ? 在 Expectation.toBeTruthy ( http://localhost:9876/base/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:2099:12)
? ? 在 ZoneDelegate.webpackJsonp.../../../../zone.js/dist/zone.js.ZoneDelegate.invoke ( http://localhost:9876/_karma_webpack_/webpack:/C:/C:/ ../webTest/node_modules/zone.js/dist/zone.js:388:1)
? ? 在 ProxyZoneSpec.webpackJsonp.../../../../zone.js/dist/proxy.js.ProxyZoneSpec.onInvoke ( http://localhost:9876/_karma_webpack_/webpack:/C:/../ webTest/node_modules/zone.js/dist/proxy.js:79:1)
? ? 在 ZoneDelegate.webpackJsonp.../../../../zone.js/dist/zone.js.ZoneDelegate.invoke ( http://localhost:9876/_karma_webpack_/webpack:/C:/../ webTest/node_modules/zone.js/dist/zone.js:387:1)
my code service:
我的代码服务:
describe('Component: Auth', () => {
? ? let component: AuthService;
? ? let fixture: ComponentFixture<AuthService>;
? ? beforeEach(() => {
? ? ? ? TestBed.configureTestingModule({
? ? ? ? ? ? declarations: [AuthService]
? ? ? ? })
? ? ? ? fixture = TestBed.createComponent(AuthService);
? ? ? ? component = fixture.componentInstance;
? ? });
});
Can you ask me, what is the problem?
你能问我,有什么问题吗?
回答by Sangwin Gawande
Use it like below :
像下面这样使用它:
Services should be in providers
array.
服务应该是providers
数组。
describe('Component: Auth', () => {
let component: AuthService;
let fixture: ComponentFixture<AuthService>;
beforeEach(() => {
TestBed.configureTestingModule({
declarations: [],
providers: [AuthService] // **Like this.**
})
fixture = TestBed.createComponent(AuthService);
component = fixture.componentInstance;
});
});
回答by jacekbj
You have to instantiate service: let service = new AuthService();
, there is no need to call "createComponent" and such.
您必须实例化 service: let service = new AuthService();
,无需调用“createComponent”等。