简体   繁体   English

如何在测试使用它的 function 时模拟 Mobx 商店的价值

[英]How to mock value of Mobx store when testing a function that uses it

I have a Mobx store that saves a token for authentication:我有一个 Mobx 商店,它保存了一个用于身份验证的令牌:

class AuthStore {
  token = null

  // ...token gets set at some point in time in a function
}

export default new AuthStore() // export singleton

and a separate function that imports this store and uses the token to make API-calls:和一个单独的 function 导入此存储并使用令牌进行 API 调用:

import { AuthStore } from '../stores'

export function createFetchHeaders() {
  return {
    Accept: 'application/json',
    'Content-Type': 'application/json',
    Authorization: `Bearer ${AuthStore.token}`,
  }
}

Now I want to write a test to make sure that the token gets used when calling this function: I tried doing this using the ES6-Class mocks described in the Jest documentation but the token is always undefined :现在我想编写一个测试以确保在调用此 function 时使用令牌:我尝试使用Jest 文档中描述的 ES6-Class 模拟来执行此操作,但令牌始终undefined

import { createFetchHeaders } from './'

const mockToken = 'foobar'
jest.mock('../../stores/AuthStore', () => {
  return jest.fn().mockImplementation(() => {
    return { token: mockToken }
  })
})

describe('createFetchHeaders()', () => {
  it('sets Bearer Authorization header using the provided token', () => {
    const headers = createFetchHeaders()

    const expected = `Bearer ${mockToken}`

    expect(headers['Authorization']).toEqual(expected)
  })
})

Where am I going wrong here?我在哪里错了? As far as I understand the aforementioned jest docs as well as this post this way of mocking the implementation should affect the tested function?!据我了解上述笑话文档以及这篇文章mocking 的实现方式应该会影响经过测试的 function?!

This is because you are exporting object instead of class.这是因为您正在导出 object 而不是 class。 So the mocking is not working.所以 mocking 不工作。 You can use javascript feature of overwriting function, object and class manually (which is internally used by jest).您可以使用 javascript 功能覆盖 function、object 和 ZA2F2ED4F8EBC2CBB4C21A29DC40AB61 手动(内部使用)。 The test code will look like this测试代码将如下所示

import AuthStore from "../../stores/AuthStore"
import {createFetchHeaders} from './';

const mockToken = 'foobar'

describe('createFetchHeaders()', () => {
    it('sets Bearer Authorization header using the provided token', () => {
        AuthStore.token = mockToken;
        const headers = createFetchHeaders()

        const expected = `Bearer ${mockToken}`

        expect(headers['Authorization']).toEqual(expected)
    })
})

Or in mocking itself you can return object instead of function或者在 mocking 本身中,您可以返回 object 而不是 function

import {createFetchHeaders} from './';

const mockToken = 'foobar'
jest.mock('../../stores/AuthStore', () => {
    return { token: 'foobar' }
})

describe('createFetchHeaders()', () => {
    it('sets Bearer Authorization header using the provided token', () => {
        const headers = createFetchHeaders()

        const expected = `Bearer ${mockToken}`

        expect(headers['Authorization']).toEqual(expected)
    })
})

And import in createFetchHeaders is wrong, which need to like this而createFetchHeaders中import是错误的,需要这样

import AuthStore from '../stores'

you don't need "{}", because you exporting AuthStore as default.您不需要“{}”,因为您将 AuthStore 作为默认值导出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM