Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/28.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 如何用茉莉花来模仿一个物体_Angular_Unit Testing_Mocking_Jasmine_Stubbing - Fatal编程技术网

Angular 如何用茉莉花来模仿一个物体

Angular 如何用茉莉花来模仿一个物体,angular,unit-testing,mocking,jasmine,stubbing,Angular,Unit Testing,Mocking,Jasmine,Stubbing,在我的DI设置中,我有一个singleton类,它有两个我想在测试时禁用的函数 在其他一些框架/语言中,我会提供一个模拟对象,该对象的函数实现是空的 我想做点像 TestBed.configureTestModule({ 进口:[……], 供应商:[ {提供:MyClass,useValue:spyAllFunctions(MyClass).and.stub()} ] }) 我已经看过了spyAllFunctions,但还没有完全理解(或者我误用了它),因为现在我只是自己提供存根,但我想知道

在我的DI设置中,我有一个singleton类,它有两个我想在测试时禁用的函数

在其他一些框架/语言中,我会提供一个模拟对象,该对象的函数实现是空的

我想做点像


TestBed.configureTestModule({
进口:[……],
供应商:[
{提供:MyClass,useValue:spyAllFunctions(MyClass).and.stub()}
]
})
我已经看过了
spyAllFunctions
,但还没有完全理解(或者我误用了它),因为现在我只是自己提供存根,但我想知道是否有比这更好的方法

{
提供:MyClass,deps:[MyClassDependency],useFactory:(dep)=>({
方法1:()=>{},
方法2()=>{}
})
}

谢谢

enno.void是对的,我认为您需要
createSpyObj
函数

let mockMyClass: any;

// first string is the name of the class (can be anything), second argument is
// an array of strings of methods you want to be able to mock
mockMyClass = jasmine.createSpyObj('my-class', ['method1', 'method2']);
.....
TestBed.configureTestModule({
    imports: […],
    providers: [
        { provide: MyClass, useValue: mockMyClass }
    ]
})

看看这个。

我想您正在寻找createSpyObj函数(),您介意为我提供一个关于如何实现我想要的功能的片段吗?我应该传入什么字符串作为第一个参数?所有的spyes都是用
()=>{}
或类似的符号来存根的吗?