Javascript 使用React Native and Continful进行笑话测试
我试图用Jest测试我的React本机应用程序。我使用Contentful作为CMS来保存我的后端信息。我目前正在尝试测试我是否正在初始化正确的客户端 以下是我用来初始化客户端的代码: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 }
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
的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”}],但未被调用正如我所说,这只是一个例子。很难说。测试失败,因为未调用该函数。您的实现是否可能实际上没有执行调用?是的,我的实现正在执行调用。您的方向正确,您需要找出问题所在。尝试在代码中添加一些日志,看看客户机对象是否是测试中的模拟对象,看看它甚至可以访问初始化代码,没有其他问题阻止它访问客户机初始化,例如,可能需要模拟其他东西我认为createClient可能需要模拟??我该怎么做?/看看这是否是问题所在