Javascript 使用React Native and Continful进行笑话测试

Javascript 使用React Native and Continful进行笑话测试,javascript,reactjs,react-native,jestjs,contentful,Javascript,Reactjs,React Native,Jestjs,Contentful,我试图用Jest测试我的React本机应用程序。我使用Contentful作为CMS来保存我的后端信息。我目前正在尝试测试我是否正在初始化正确的客户端 以下是我用来初始化客户端的代码: var client = contentful.createClient({ space: 'w20789877', // whatever the space id is accessToken: '883829200101001047474747737' // some accessToken }

我试图用Jest测试我的React本机应用程序。我使用Contentful作为CMS来保存我的后端信息。我目前正在尝试测试我是否正在初始化正确的客户端

以下是我用来初始化客户端的代码:

var client = contentful.createClient({
  space: 'w20789877',  // whatever the space id is 
  accessToken: '883829200101001047474747737' // some accessToken
})
describe ('should initialize the correct client', () => {(
   it('should initialize the correct client info from contentful', () =>{
      expect(client).toEqual('w20789877', '883829200101001047474747737')
});
)};
以下是我用来测试初始化客户端的代码:

var client = contentful.createClient({
  space: 'w20789877',  // whatever the space id is 
  accessToken: '883829200101001047474747737' // some accessToken
})
describe ('should initialize the correct client', () => {(
   it('should initialize the correct client info from contentful', () =>{
      expect(client).toEqual('w20789877', '883829200101001047474747737')
});
)};
但是,我收到一条错误消息,指出:

Difference: Comparing two different types of values. Expected undefined but received string.

出于某种原因,我收到了未定义的空间和accessToken,但我正确地初始化了客户端,因为我以后可以使用空间。即使尝试打印出空格和accessToken,也会打印未定义的值。

这里有几个问题:

  • toEqual
    匹配器接收单个值参数;你发送了2个 参数,因此有效地只使用第一个参数
  • 在本例中,
    客户端
    是一个函数,您试图将其与字符串进行比较。不管客户机不是字符串,在您的例子中,它也是
    未定义的
    ,因此会显示消息“预期未定义但已接收字符串”。这里您不是在测试空间或accessToken,而是在测试客户端
  • 我不完全确定您在这里测试的是什么,但这与Contentful没有特别的关系

    我假设客户机初始化部分位于您想要进行单元测试的代码中的某个地方(而不是在测试文件中初始化)。我建议进行一项测试,检查在执行代码时,
    contentful
    createClient
    函数是否使用预期参数被调用;无需测试客户机是否已创建——这是Contentful的责任,以确保它们返回有效的客户机对象。重要的是,您要传递应用程序所需的正确“空格”和“accessToken”参数

    一般来说,应该模拟外部服务,并且您应该只测试自己的逻辑以及和外部服务的交互

    例子 为了简单起见,让我们假设初始化客户端的代码如下所示:

    //client.js
    
    var contentful = require('contentful')
    
    export default function initializeClient() {
        var client = contentful.createClient({
          space: 'w20789877',  // whatever the space id is 
          accessToken: '883829200101001047474747737' // some accessToken
        });
    }
    
    //client.test.js
    
    describe('contentful client', () => {
        let contentful;
        let initializeClient;
    
        beforeEach(() => {
            jest.mock('contentful');
    
            contentful = require('contentful');
            initializeClient = require('./client').default;
        });
    
        it('should initialize the contentful client with the correct params', async () => {
            initializeClient();
            expect(contentful.createClient).toHaveBeenCalledWith({
                space: 'w20789877',
                accessToken: '883829200101001047474747737'
            });
        });
    });
    
    测试可能如下所示:

    //client.js
    
    var contentful = require('contentful')
    
    export default function initializeClient() {
        var client = contentful.createClient({
          space: 'w20789877',  // whatever the space id is 
          accessToken: '883829200101001047474747737' // some accessToken
        });
    }
    
    //client.test.js
    
    describe('contentful client', () => {
        let contentful;
        let initializeClient;
    
        beforeEach(() => {
            jest.mock('contentful');
    
            contentful = require('contentful');
            initializeClient = require('./client').default;
        });
    
        it('should initialize the contentful client with the correct params', async () => {
            initializeClient();
            expect(contentful.createClient).toHaveBeenCalledWith({
                space: 'w20789877',
                accessToken: '883829200101001047474747737'
            });
        });
    });
    

    注意:我没有实际运行或测试上述代码,但这是一般概念。

    谢谢您的帮助!我能够运行并测试代码。但是,测试失败,原因是:
    expect(jest.fn())。toHaveBeenCalledWith(expected)预期模拟函数已被调用:[{“accessToken”:“8838292001010010474747737”,“space”:“w20789877”}],但未被调用