英文:
I got and error: TypeError: this.todos$.pipe is not a function when create an jasmine.SpyObj as a fack service
问题
这是一个简单的待办事项列表组件:
这是我的测试用例:
fdescribe('TodoListComponent With FackService', () => {
let fixture: ComponentFixture<TodolistComponent>;
let component: TodolistComponent;
let fackTodoService: jasmine.SpyObj<TodoService>;
beforeEach(async () => {
fackTodoService = jasmine.createSpyObj<TodoService>('TodoService', {
todolist$: of(mocktodos),
currentTodoList: [],
getTodos: of(mocktodos),
deleteTodo: undefined,
addTodo: of(mocktodos[0]),
});
await TestBed.configureTestingModule({
declarations: [TodolistComponent],
providers: [
{
provide: TodoService,
useValue: fackTodoService,
},
],
}).compileComponents();
fixture = TestBed.createComponent(TodolistComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should do something', () => {});
});
触发这个测试后,它报告了以下错误:
似乎模拟的服务在创建 fixture 时无法初始化这些数据...
我能得到一些帮助吗?我找不到解决办法... 谢谢大家~!
英文:
Here is a simple todo list component:
Here is my test case:
fdescribe('TodoListComponent With FackService', () => {
let fixture: ComponentFixture<TodolistComponent>;
let component: TodolistComponent;
let fackTodoService: jasmine.SpyObj<TodoService>;
beforeEach(async () => {
fackTodoService = jasmine.createSpyObj<TodoService>('TodoService', {
todolist$: of(mocktodos),
currentTodoList: [],
getTodos: of(mocktodos),
deleteTodo: undefined,
addTodo: of(mocktodos[0]),
});
await TestBed.configureTestingModule({
declarations: [TodolistComponent],
providers: [
{
provide: TodoService,
useValue: fackTodoService,
},
],
}).compileComponents();
fixture = TestBed.createComponent(TodolistComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should do something', () => {});
});
after I trigger this test, it report an error as follow:
It seems the mocked service cannot init these data when creating the fixture...
Can I get some help for it, I cannot find effect solution for it... Thank you gays~!
答案1
得分: 1
第二个参数在jasmine.createSpyObj
中用于方法,第三个参数用于实例变量
。
所以想象一下,这是您想要模拟的内容:
export class AnyThing {
instanceVariable = 2;
method() {
return 3;
}
}
它将是:
let mockAnything: jasmine.SpyObj<Anything>;
...
mockAnything = jasmine.createSpyObj<Anything>('Anything', { method: jasmine.createSpy() }, { instanceVariable: 2 });
也可以缩写为:
mockAnything = jasmine.createSpyObj<Anything>('Anything', ['method'], { instanceVariable: 2 });
所有这些都是为了说,将这一行更改为:
fackTodoService = jasmine.createSpyObj<TodoService>('TodoService', {
todolist$: of(mocktodos),
currentTodoList: [],
getTodos: of(mocktodos),
deleteTodo: undefined,
addTodo: of(mocktodos[0]),
});
变成这样:
// 在'TodoService'后面添加一个额外的对象来模拟方法
fackTodoService = jasmine.createSpyObj<TodoService>('TodoService', {}, {
todolist$: of(mocktodos),
currentTodoList: [],
getTodos: of(mocktodos),
deleteTodo: undefined,
addTodo: of(mocktodos[0]),
});
todolist$
是一个实例变量而不是方法,这就是为什么我们需要使用第三个参数。
这是一些关于您想要的文档:https://stackoverflow.com/q/64560390/7365461
英文:
The 2nd argument here in jasmine.createSpyObj
is for methods, the third argument is for instance variables
.
So imagine this is what you want to mock:
export class AnyThing {
instanceVariable = 2;
method() {
return 3;
}
}
It would be:
let mockAnything: jasmine.SpyObj<Anything>;
...
mockAnything = jasmine.createSpyObj<Anything>('Anything', { method: jasmine.createSpy() }, { instanceVariable: 2 });
It can be shortened to:
mockAnything = jasmine.createSpyObj<Anything>('Anything', ['method'], { instanceVariable: 2 });
All that to say, change this line:
fackTodoService = jasmine.createSpyObj<TodoService>('TodoService', {
todolist$: of(mocktodos),
currentTodoList: [],
getTodos: of(mocktodos),
deleteTodo: undefined,
addTodo: of(mocktodos[0]),
});
To this:
// Add an extra object here for the mock methods after 'TodoService'
fackTodoService = jasmine.createSpyObj<TodoService>('TodoService', {}, {
todolist$: of(mocktodos),
currentTodoList: [],
getTodos: of(mocktodos),
deleteTodo: undefined,
addTodo: of(mocktodos[0]),
});
todolist$
is an instance variable and not a method and that's why we need to use the 3rd argument.
Here is some documentation on what you would like: https://stackoverflow.com/q/64560390/7365461
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论