- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
使用 TestBed ,我们能够为依赖注入(inject)可用的类创建模拟类。例如,MyButtonClass
可以访问 ElementRef
和 MyService
,因为它们是通过依赖注入(inject)实现的,因此我们可以覆盖它们。我遇到的问题是,要编写 Jasmine 测试,我必须创建模拟类来覆盖无法通过依赖注入(inject)访问的类的方法。
在这种情况下,ScriptLoader.load
将在全局空间中加载 ThirdPartyCheckout
。这意味着,当 Jasmine 读取订阅操作符中的内容时,它可能不可用。出于这个原因,我想先 mock 前者,然后再 mock 后者。或者也许有不同的方法来解决这个问题。
如果有人可以建议一种方法来创建模拟类来覆盖 ScriptLoader.load
方法和 ThirdPartyCheckout.configure
方法,那就太好了。
要测试的指令:
@Directive({
selector: '[myButton]'
})
export class MyButtonClass implements AfterViewInit {
private myKey: string;
constructor(private _el: ElementRef, private myService: MyService) {}
ngAfterViewInit() {
this.myService.getKey()
.then((myKey: string) => {
this.myKey = myKey;
ScriptLoader.load('https://thirdpartyurl.js', this._el.nativeElement)
.subscribe(
data => {
this.handeler = ThirdPartyCheckout.configure(<any>{
key: this.myKey
// etc
// and some methods go here
});
},
error => {
console.log(error);
}
);
});
}
}
测试代码如下:
@Component({
selector: 'test-cmp',
template: ''
})
class TestComponent {}
class mockMyService {
getKey() {
return Promise.resolve('this is a key in real code');
}
}
describe('myButton', () => {
beforeEach(() => {
TestBed.configureTestingModule({
declarations: [TestComponent, MyButtonClass],
providers: [
{provide: MyService, useClass: mockMyService}
]
});
});
describe('ngAfterViewInit', fakeAsync(() => {
const template = '<div><div myButton></div></div>';
TestBed.overrideComponent(TestComponent, {set: {template: template}});
let fixture = TestBed.createComponent(TestComponent);
fixture.detectChanges();
tick();
}));
});
最佳答案
函数是一等公民,你可以给它分配一个新函数
let originalFn;
beforeEach(() => {
originalFn = ScriptLoader.load;
});
afterEach(() => {
ScriptLoader.load = originalFn;
});
it('...', fakeAsync(() => {
ScriptLoader.load = (url, el: Element): Observable<string> => {
return Observable.of('HelloSquirrel');
};
...
}));
除此之外,您可能只想考虑使用 DI。使用 DI 的主要原因之一是为了更好的可测试性。对于 ScriptLoader
只需将方法设为非静态方法,对于第三方库只需为其创建抽象服务层即可。
关于unit-testing - Angular 2 TestBed,没有依赖注入(inject)的模拟方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39490379/
我有一个组件,它有两个依赖项:一个是 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:
我是一名优秀的程序员,十分优秀!