I got and error: TypeError: this.todos$.pipe is not a function when create an jasmine.SpyObj as a fack service

huangapple go评论69阅读模式
英文:

I got and error: TypeError: this.todos$.pipe is not a function when create an jasmine.SpyObj as a fack service

问题

这是一个简单的待办事项列表组件:
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', () => {});
});

触发这个测试后,它报告了以下错误:
I got and error: TypeError: this.todos$.pipe is not a function when create an jasmine.SpyObj as a fack service

似乎模拟的服务在创建 fixture 时无法初始化这些数据...
我能得到一些帮助吗?我找不到解决办法... 谢谢大家~!

英文:

Here is a simple todo list component:
I got and error: TypeError: this.todos$.pipe is not a function when create an jasmine.SpyObj as a fack service
Here is my test case:

fdescribe(&#39;TodoListComponent With FackService&#39;, () =&gt; {
  let fixture: ComponentFixture&lt;TodolistComponent&gt;;
  let component: TodolistComponent;
  let fackTodoService: jasmine.SpyObj&lt;TodoService&gt;;

  beforeEach(async () =&gt; {
    fackTodoService = jasmine.createSpyObj&lt;TodoService&gt;(&#39;TodoService&#39;, {
      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(&#39;should do something&#39;, () =&gt; {});
});

after I trigger this test, it report an error as follow:
I got and error: TypeError: this.todos$.pipe is not a function when create an jasmine.SpyObj as a fack service

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]),
    });

变成这样:

// 在&#39;TodoService&#39;后面添加一个额外的对象来模拟方法
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&lt;Anything&gt;;
...
mockAnything = jasmine.createSpyObj&lt;Anything&gt;(&#39;Anything&#39;, { method: jasmine.createSpy() }, { instanceVariable: 2 });

It can be shortened to:

mockAnything = jasmine.createSpyObj&lt;Anything&gt;(&#39;Anything&#39;, [&#39;method&#39;], { instanceVariable: 2 });

All that to say, change this line:

fackTodoService = jasmine.createSpyObj&lt;TodoService&gt;(&#39;TodoService&#39;, {
      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 &#39;TodoService&#39;
fackTodoService = jasmine.createSpyObj&lt;TodoService&gt;(&#39;TodoService&#39;, {}, {
      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

huangapple
  • 本文由 发表于 2023年5月25日 07:50:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/76328051.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定