开玩笑嘲笑多个类实例

发布于 2025-01-12 04:34:46 字数 2059 浏览 0 评论 0 原文

我有一个实例化同一对象的多个实例的类,我想开玩笑地模拟它,但我不断收到第二个 it(()=>{})('it should create 第二个问候语')

错误: Expect(received).toBe(expected) // Object.is 相等

预期:“这是第二个问候语”

收到的:“这是第一个问候语”

预期:“Hello and Good Evening”

收到的:“Hello和早安”

import {Greeting} from 'somefile/greeting'

interface FooProps {
  myProps: string
}

class Foo {
  private greeting1: Greeting;
  private greeting2: Greeting;

  constructor(bar: Bar, id: string, props: FooProps) {

    this.greeting1 = new Greeting(bar, `${id}-first-greeting`, {
      prop1: 'Hello and Good Morning',
      prop2: {
        source: ['random']
      }
    })

    this.greeting2 = new Greeting(bar, `${id}-second-greeting`, {
      prop1: 'Hello and Good Evening',
      prop2: {
        source: ['anotherRandom']
      }
    })
  }
}
import Foo from 'somefile/foo'
import {Greeting} from 'somefile/greeting'
 
jest.mock('somefile/greeting');
const FirstMock = mocked(Greeting, true);
const SecondMock = mocked(Greeting, true);

const id = 'This is';

describe('Greeting', () => {
  let bar: Bar;
  let foo: Foo;
  let random1 = ['random'];
  let random2 = ['anotherRandom'];

  const props: FooProps = {
    myProps: 'myProps'
  }
  
  beforeEach( () => {
    bar = new Bar();
    foo = new Foo(bar, id, props);
  })

  afterEach( () => {
    jest.clearAllMocks();
  })

  it('should create first greeting', () => {
    expect(random.mock.calls[0][0]).toBe(bar)
    expect(random.mock.calls[0][1]).toBe(`${id}-first-greeting`)
    expect(random.mock.calls[0][2]?.prop1).toBe('Hello and Good Morning')
    expect(random.mock.calls[0][2]?.prop2.source).toBe(random1)
  })

  it('should create second greeting', () => {
    expect(random.mock.calls[0][0]).toBe(bar)
    expect(random.mock.calls[0][1]).toBe(`${id}-second-greeting`)
    expect(random.mock.calls[0][2]?.prop1).toBe('Hello and Good Evening')
    expect(random.mock.calls[0][2]?.prop2.source).toBe(random2)
  })  

})

第一个测试通过了,但第二个测试总是失败。看起来它保留了旧测试的值。这个问题可以解决吗?

I have class that instantiates multiple instances of the same object and I want to mock this out in jest, But I keep getting error for the second it(()=>{})('it should create second greeting')

Error: expect(received).toBe(expected) // Object.is equality

Expected: "This is the-second-greeting"

Received: "This is the-first-greeting"

Expected: "Hello and Good Evening"

Received: "Hello and Good Morning"

import {Greeting} from 'somefile/greeting'

interface FooProps {
  myProps: string
}

class Foo {
  private greeting1: Greeting;
  private greeting2: Greeting;

  constructor(bar: Bar, id: string, props: FooProps) {

    this.greeting1 = new Greeting(bar, `${id}-first-greeting`, {
      prop1: 'Hello and Good Morning',
      prop2: {
        source: ['random']
      }
    })

    this.greeting2 = new Greeting(bar, `${id}-second-greeting`, {
      prop1: 'Hello and Good Evening',
      prop2: {
        source: ['anotherRandom']
      }
    })
  }
}
import Foo from 'somefile/foo'
import {Greeting} from 'somefile/greeting'
 
jest.mock('somefile/greeting');
const FirstMock = mocked(Greeting, true);
const SecondMock = mocked(Greeting, true);

const id = 'This is';

describe('Greeting', () => {
  let bar: Bar;
  let foo: Foo;
  let random1 = ['random'];
  let random2 = ['anotherRandom'];

  const props: FooProps = {
    myProps: 'myProps'
  }
  
  beforeEach( () => {
    bar = new Bar();
    foo = new Foo(bar, id, props);
  })

  afterEach( () => {
    jest.clearAllMocks();
  })

  it('should create first greeting', () => {
    expect(random.mock.calls[0][0]).toBe(bar)
    expect(random.mock.calls[0][1]).toBe(`${id}-first-greeting`)
    expect(random.mock.calls[0][2]?.prop1).toBe('Hello and Good Morning')
    expect(random.mock.calls[0][2]?.prop2.source).toBe(random1)
  })

  it('should create second greeting', () => {
    expect(random.mock.calls[0][0]).toBe(bar)
    expect(random.mock.calls[0][1]).toBe(`${id}-second-greeting`)
    expect(random.mock.calls[0][2]?.prop1).toBe('Hello and Good Evening')
    expect(random.mock.calls[0][2]?.prop2.source).toBe(random2)
  })  

})

The first test passes, but the second one always fails. It looks like it is retaining the values of the old test. Can this be fixed?

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

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

发布评论

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

评论(1

败给现实 2025-01-19 04:34:46

您应该使用 jest.resetAllMocks();

clearAllMocks 从字面上删除模拟的状态,因此您将丢失您设置的所有值。

resetAllMocks 会将模拟返回到其原始状态。

You should use jest.resetAllMocks();

clearAllMocks literally removes the state of your mock, so you are losing all the values you've setup.

Whereas resetAllMocks will return the mock to its original state.

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