嘲笑茉莉花中的蜘蛛对象

发布于 2025-01-23 03:57:00 字数 889 浏览 3 评论 0原文

我的组件订阅了类似服务的可观察到的服务:

  ngOnInit(): void {
    this.sub.add(
      this.notesService.notes$.subscribe(notes => {
        this.notes = notes.map(note => {
          let profilePicture = generateProfilePicture(note.creator)
            return {
              ...note,
              color: profilePicture.color,
              initials: profilePicture.initials
            }
        })
      })
    );
  } 

在我的单元测试中,我为笔记服务创建了一个spyobject。我该如何存根note $可观察?

providers: [
        ConfirmationService,
        {
          provide: HttpClient,
          useValue: jasmine.createSpyObj("HttpClient", ["get", "post", "put", "delete"])
        },
        {
          provide: NotesService,
          useValue: jasmine.createSpyObj("NotesService",
            ["addNote", "getNotesByRefId", "editNote", "deleteNote"])
        }
      ]

My component subscribes to an observable of a service like that:

  ngOnInit(): void {
    this.sub.add(
      this.notesService.notes$.subscribe(notes => {
        this.notes = notes.map(note => {
          let profilePicture = generateProfilePicture(note.creator)
            return {
              ...note,
              color: profilePicture.color,
              initials: profilePicture.initials
            }
        })
      })
    );
  } 

In my unit test I created a spyobject for the NoteService. How can I stub the note$ observable?

providers: [
        ConfirmationService,
        {
          provide: HttpClient,
          useValue: jasmine.createSpyObj("HttpClient", ["get", "post", "put", "delete"])
        },
        {
          provide: NotesService,
          useValue: jasmine.createSpyObj("NotesService",
            ["addNote", "getNotesByRefId", "editNote", "deleteNote"])
        }
      ]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

注定孤独终老 2025-01-30 03:57:00

分机分开(需要在需要时使用):


const trigger = new Subject<any>();
let trigger$: Observable<any>;

beforeEach(() => {
  trigger$ = trigger.asObservable().pipe(first());
  component.notesService.notes$ = trigger$;
})

it('Should XXX', () => {
  trigger$.subscribe(() => {
    expect(...);
  });

  component.ngOnOnInit();

  trigger.next('Value that the observable should emit');
})

Typings apart (use as any when needed) :


const trigger = new Subject<any>();
let trigger$: Observable<any>;

beforeEach(() => {
  trigger$ = trigger.asObservable().pipe(first());
  component.notesService.notes$ = trigger$;
})

it('Should XXX', () => {
  trigger$.subscribe(() => {
    expect(...);
  });

  component.ngOnOnInit();

  trigger.next('Value that the observable should emit');
})

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文