angular - 错误:StaticInjectorError(DynamicTestModule)[NotificationsComponent
问题描述
运行时完全错误ng test
:
错误:StaticInjectorError(DynamicTestModule)[NotificationsComponent
AuthService]:StaticInjectorError(平台:核心)[NotificationsComponent AuthService]:
NullInjectorError:没有 AuthService 的提供者!
预计未定义是真实的。
错误:预期未定义是真实的。
在堆栈(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 )
我的代码服务:
describe('Component: Auth', () => {
let component: AuthService;
let fixture: ComponentFixture<AuthService>;
beforeEach(() => {
TestBed.configureTestingModule({
declarations: [AuthService]
})
fixture = TestBed.createComponent(AuthService);
component = fixture.componentInstance;
});
});
你能问我,有什么问题吗?
解决方案
像下面这样使用它:
服务应该在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;
});
});
推荐阅读
- node.js - 如何修复这个经常改变我的机器人状态的代码?
- asp.net - 从客户端验证属性获取对象
- c++ - Qt deploy 找不到 Visual Studio 安装目录
- reactjs - React.js 路由器 URL 正在从浏览器解析,但不是从 App 内的链接解析
- database - React Native - Sqlite 加密
- spring-kafka - 我们可以将独立的 Spring Cloud Schema Registry 与 Confluent 的 KafkaAvroSerializer 一起使用吗?
- laravel - Laravel 减少迁移文件
- firefox - navigator.share 在桌面 Firefox 上设置,但不执行任何操作
- c# - 如何从项目服务依赖项访问 Azure SQL 连接字符串
- fasm - FASM 程序以未经授权的访问错误结束