茉莉花:在嵌套功能中使用间谍(对于AngularFirestore)

发布于 2025-02-05 04:28:44 字数 678 浏览 4 评论 0原文

我像这样嘲笑AngularFirestore:

beforeEach(() => {
  TestBed.configureTestingModule({
    {
      providers: [
        {
          provide: AngularFirestore,
          useValue: {
            collection: () => ({
              doc: () => ({
                set: () => new Promise((resolve) => resolve(void 0))})
              })
            }
          }
        }
      ]
    }
  })
})

collection()返回包含doc属性的对象。 Collection()。doc()返回一个函数,包含set属性。 collection()。doc()。set()返回void Promise

如何使用spyon在调用set的调用中?

I am mocking AngularFirestore like this:

beforeEach(() => {
  TestBed.configureTestingModule({
    {
      providers: [
        {
          provide: AngularFirestore,
          useValue: {
            collection: () => ({
              doc: () => ({
                set: () => new Promise((resolve) => resolve(void 0))})
              })
            }
          }
        }
      ]
    }
  })
})

collection() returns an object containing a doc property. collection().doc() returns a function, containing a set property. collection().doc().set() returns a void Promise.

How do I use spyOn to spy on calls to set?

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

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

发布评论

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

评论(1

热鲨 2025-02-12 04:28:44

spyon让您间谍在一个对象拥有的一个属性上。您必须:

  1. 间谍目标
  2. 茉莉间谍对象的
  3. 建立基于#1嵌入#1的

,例如这样

let firestoreSpy: jasmine.Spy;
beforeEach(() => {
  const firestoreGet = {
    set: () => new Promise((resolve) => resolve(void 0))
  };
  firestoreSpy = spyOn(firestoreGet, 'set');
  TestBed.configureTestingModule({
    {
      providers: [
        {
          provide: AngularFirestore,
          useValue: {
            collection: () => ({
              doc: () => firestoreGet
            }
          }
        }
      ]
    }
  })
})
it('mister it', () => expect(firestoreSpy.[what have you]).[to be whatever]);

spyOn lets you spy on one property owned by one object. You have to:

  1. Build the spy target by itself
  2. Build the Jasmine spy object based on #1
  3. Embed #1 into the mocked Firestore

Like this:

let firestoreSpy: jasmine.Spy;
beforeEach(() => {
  const firestoreGet = {
    set: () => new Promise((resolve) => resolve(void 0))
  };
  firestoreSpy = spyOn(firestoreGet, 'set');
  TestBed.configureTestingModule({
    {
      providers: [
        {
          provide: AngularFirestore,
          useValue: {
            collection: () => ({
              doc: () => firestoreGet
            }
          }
        }
      ]
    }
  })
})
it('mister it', () => expect(firestoreSpy.[what have you]).[to be whatever]);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文