- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试测试一个具有依赖性的简单组件。我试图模拟这种依赖关系,但真正的服务仍在构建中。有人能看出我错过了什么吗?
这是我的组件:
import { Component } from "@angular/core";
import { AuthService } from "src/app/services/auth.service.js";
@Component({
selector: "app-auth-form",
template: ""
})
export class AuthFormComponent {
constructor(private authService: AuthService) {}
}
这取决于这个AuthService
:
import { Injectable } from "@angular/core";
import { HttpClient } from "@angular/common/http";
import { UserModule } from "../user/user.module";
@Injectable({
providedIn: "root"
})
export class AuthService {
constructor(private http: HttpClient) {}
registerUser(email: string, password: string): Promise<UserModule> {
return null;
}
}
请注意,它又依赖于 HttpClient
。
现在我的测试看起来像这样:
import { async, TestBed } from "@angular/core/testing";
import { AuthFormComponent } from "./auth-form.component";
import { AuthService } from "src/app/services/auth.service";
describe("AuthFormComponent", () => {
beforeEach(async(() => {
const authServiceSpy = jasmine.createSpyObj("AuthService", [
"registerUser"
]);
TestBed.configureTestingModule({
declarations: [AuthFormComponent],
providers: [{ provide: AuthService, useValue: authServiceSpy }]
}).compileComponents();
}));
it("should create", () => {
let component = TestBed.createComponent(AuthFormComponent)
.componentInstance;
expect(component).toBeTruthy();
});
});
这里请看 { provide: AuthService, useValue: authServiceSpy }
行,因此我希望不会构造真正的 AuthService
。但是当我运行测试时,出现以下错误:
Chrome 78.0.3904 (Windows 10.0.0) AuthFormComponent should create FAILED
NullInjectorError: StaticInjectorError(DynamicTestModule)[HttpClient]:
StaticInjectorError(Platform: core)[HttpClient]:
NullInjectorError: No provider for HttpClient!
error properties: Object({ ngTempTokenPath: null, ngTokenPath: [ Function ], ngDebugContext: DebugContext_({ view: Object({ def: Object({ factory: Function, nodeFlags: 33603585, rootNodeFlags: 33554433, nodeMatchedQueries: 0, flags: 0, nodes: [ Object({ nodeIndex: 0, parent: null, renderParent: null, bindingIndex: 0, outputIndex: 0, checkIndex: 0, flags: 33554433, childFlags: 49152, directChildFlags: 49152, childMatchedQueries: 0, matchedQueries: Object({ }), matchedQueryIds: 0, references: Object({ }), ngContentIndex: null, childCount: 1, bindings: [ ], bindingFlags: 0, outputs: [ ], element: Object({ ns: '', name: 'app-auth-form', attrs: [ ], template: null, componentProvider: Object({ nodeIndex: 1, parent: <circular reference: Object>, renderParent: <circular reference: Object>, bindingIndex: 0, outputIndex: 0, checkIndex: 1, flags: 49152, childFlags: 0, directChildFlags: 0, childMatchedQueries: 0, matchedQueries: Object, matchedQueryIds: 0, references: Object, ngContentIndex: -1, childCount: 0 ...
at <Jasmine>
at NullInjector.get (http://localhost:9876/_karma_webpack_/node_modules/@angular/core/fesm2015/core.js:855:1)
at resolveToken (http://localhost:9876/_karma_webpack_/node_modules/@angular/core/fesm2015/core.js:17514:1)
at tryResolveToken (http://localhost:9876/_karma_webpack_/node_modules/@angular/core/fesm2015/core.js:17440:1)
at StaticInjector.get (http://localhost:9876/_karma_webpack_/node_modules/@angular/core/fesm2015/core.js:17266:1)
at resolveToken (http://localhost:9876/_karma_webpack_/node_modules/@angular/core/fesm2015/core.js:17514:1)
at tryResolveToken (http://localhost:9876/_karma_webpack_/node_modules/@angular/core/fesm2015/core.js:17440:1)
at StaticInjector.get (http://localhost:9876/_karma_webpack_/node_modules/@angular/core/fesm2015/core.js:17266:1)
at resolveNgModuleDep (http://localhost:9876/_karma_webpack_/node_modules/@angular/core/fesm2015/core.js:30393:1)
at NgModuleRef_.get (http://localhost:9876/_karma_webpack_/node_modules/@angular/core/fesm2015/core.js:31578:1)
at injectInjectorOnly (http://localhost:9876/_karma_webpack_/node_modules/@angular/core/fesm2015/core.js:734:1)
Chrome 78.0.3904 (Windows 10.0.0): Executed 3 of 3 (1 FAILED) (0 secs / 0.077 secs)
Chrome 78.0.3904 (Windows 10.0.0) AuthFormComponent should create FAILED
NullInjectorError: StaticInjectorError(DynamicTestModule)[HttpClient]:
StaticInjectorError(Platform: core)[HttpClient]:
NullInjectorError: No provider for HttpClient!
error properties: Object({ ngTempTokenPath: null, ngTokenPath: [ Function ], ngDebugContext: DebugContext_({ view: Object({ def: Object({ factory: Function, nodeFlags: 33603585, rootNodeFlags: 33554433, nodeMatchedQueries: 0, flags: 0, nodes: [ Object({ nodeIndex: 0, parent: null, renderParent: null, bindingIndex: 0, outputIndex: 0, checkIndex: 0, flags: 33554433, childFlags: 49152, directChildFlags: 49152, childMatchedQueries: 0, matchedQueries: Object({ }), matchedQueryIds: 0, references: Object({ }), ngContentIndex: null, childCount: 1, bindings: [ ], bindingFlags: 0, outputs: [ ], element: Object({ ns: '', name: 'app-auth-form', attrs: [ ], template: null, componentProvider: Object({ nodeIndex: 1, parent: <circular reference: Object>, renderParent: <circular reference: Object>, bindingIndex: 0, outputIndex: 0, checkIndex: 1, flags: 49152, childFlags: 0, directChildFlags: 0, childMatchedQueries: 0, matchedQueries: Object, matchedQueryIds: 0, references: Object, ngContentIndex: -1, childCount: 0 ...
at <Jasmine>
at NullInjector.get (http://localhost:9876/_karma_webpack_/node_modules/@angular/core/fesm2015/core.js:855:1)
at resolveToken (http://localhost:9876/_karma_webpack_/node_modules/@angular/core/fesm2015/core.js:17514:1)
at tryResolveToken (http://localhost:9876/_karma_webpack_/node_modules/@angular/core/fesm2015/core.js:17440:1)
at StaticInjector.get (http://localhost:9876/_karma_webpack_/node_modules/@angular/core/fesm2015/core.js:17266:1)
at resolveToken (http://localhost:9876/_karma_webpack_/node_modules/@angular/core/fesm2015/core.js:17514:1)
at tryResolveToken (http://localhost:9876/_karma_webpack_/node_modules/@angular/core/fesm2015/core.js:17440:1)
at StaticInjector.get (http://localhost:9876/_karma_webpack_/node_modules/@angular/core/fesm2015/core.js:17266:1)
at resolveNgModuleDep (http://localhost:9876/_karma_webpack_/node_modules/@angular/core/fesm2015/core.js:30393:1)
at NgModuleRef_.get (http://localhost:9876/_karma_webpack_/node_modules/@angular/core/fesm2015/core.js:31578:1)
Chrome 78.0.3904 (Windows 10.0.0): Executed 3 of 3 (1 FAILED) (0.096 secs / 0.077 secs)
在我看来,这似乎是它试图构建真正的 AuthService
,但没有它的依赖项。任何指针都会很棒!
编辑
我在 StarBlitz 中重新创建了场景 - https://angular-wrk4yi.stackblitz.io , 但 它在那里工作正常。所以它一定在我这边的某个地方。
最佳答案
编辑
终于明白了!问题出在导入上。在我的组件中我有:
import { AuthService } from "src/app/services/auth.service.js";
代替:
import { AuthService } from "src/app/services/auth.service";
然后我假设它从 dist 文件夹加载 javascript 版本,然后提供的类型化基础不起作用。
我无法弄清楚是什么提供了原始服务,但找到了这个解决方法:
TestBed.overrideProvider(AuthService, { useValue: authServiceSpy})
这根据名称覆盖真正的服务提供者来自的地方。而且我不再需要支持真正的 AuthService 依赖项 + 主要是被测类实际上在这里使用我的 spy !
关于angular - 为什么我的 TestBed 提供者不覆盖实际服务?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59444096/
我有一个组件,它有两个依赖项:一个是 LOCALE_ID在 Angular 中全局定义,另一种是语言,在组件中定义为 { provide: LANGUAGE_TOKEN, useValue: navi
我正在尝试测试一个具有依赖性的简单组件。我试图模拟这种依赖关系,但真正的服务仍在构建中。有人能看出我错过了什么吗? 这是我的组件: import { Component } from "@angula
import java.util.Random; public class TestBed { public static void main(String a[]) { // creatin
我目前正在尝试为在其 html 中使用另一个组件标记的组件编写一组单元测试。 但是我似乎无法编译这个其他组件。我尝试了几种使用 NO_ERROR_SCHEMA 的方
我有 MainComponent,它使用 ChildComponentA 作为 @ViewChild。 MainComponent 正在调用 ChildComponentA 上的方法。 我想编写一个模
当对测试夹具使用以下配置时,我收到无法找到标签的投诉。直接在 AppModule 中替换 MockSelectionToolComponent 工作正常,所以一定是别的东西...... // Add
在基于 Angular 8.1.2 和 Ionic 4 的应用程序项目中,我为 typescript 中的一个简单类编写了单元测试。这与“npm test”配合得很好。为了准备需要模拟的更复杂的类,我
可以说我有一个如下所示的测试配置 TestBed.configureTestingModule({ imports: [HttpClientTestingModule],
我正在尝试测试在 App Engine 上运行的应用程序。我正在使用 the Testbed framework ,到目前为止,除了以下意外行为外,它就像一个魅力: 像这样的测试将工作得很好(没有框架
如何使用另一个提供者的实例在 TestBed.configureTestingModule() 中实例化一个提供者? 一个例子(显然不起作用): beforeEach(() => { TestBe
我正在使用 TestBed 进行 Angular 2+ 单元测试。场景,我想验证我的组件,即伪元素的颜色。 组件.ts label::before { right: 0;
我想了解这个 testb 指令 (x86-64) 会做什么。 testb $1, %al 这里 $1 的值(value)是多少。是全部 (0xFF) 还是单个 1 (0x1)? 程序集由 clang
关闭。这个问题是opinion-based .它目前不接受答案。 想要改进这个问题? 更新问题,以便 editing this post 可以用事实和引用来回答它. 关闭 5 年前。 Improve
我正在编写 TestBed 单元测试。 有一个特定的组件,它是被测组件的子组件。该子组件在测试运行时会导致错误。该子组件与测试本身无关;这只会引起问题。 我想用一个虚拟的替换它,或者阻止它被添加。 有
我是 Angular 2 的 Jasmine 新手,在编写测试用例时我经常使用 TestBed 对象并收到错误:请在测试前调用“TestBed.compileComponents”。 如何解决这个错误
我们正在使用 TestBed.overrideComponent(CoolComponent, { set: { template: 'i am the fake componen
我使用 Angular TestBed 进行了一些单元测试。即使测试非常简单,它们运行起来也非常慢(平均每秒 1 个测试 Assets )。 即使在重新阅读 Angular 文档后,我也找不到性能如此
我有一个组件,它接收组件的组件类以动态创建为子组件。 let componentFactory = this.componentFactoryResolver.resolveComponentFact
我正在使用带有 Jest 的 Angular 7 并且正在模拟组件的提供者。很多时候我需要更改在 TestBed 之后注入(inject)到组件中的内容。已编译,并且使用此代码执行此操作没有任何问题:
我实际上正在尝试构建一个 flutter 应用程序窗口,但我收到了这条错误消息: flutter build windows CMake Error:
我是一名优秀的程序员,十分优秀!