Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jestjs 开玩笑+;React本机测试库:如何测试图像src?_Jestjs_React Native Testing Library - Fatal编程技术网

Jestjs 开玩笑+;React本机测试库:如何测试图像src?

Jestjs 开玩笑+;React本机测试库:如何测试图像src?,jestjs,react-native-testing-library,Jestjs,React Native Testing Library,在我新的React原生应用程序中,我想添加一些Jest测试 一个组件渲染背景图像,该图像直接位于项目的assets文件夹中 现在,我不知道如何测试该图像是否确实是从该路径获取的,因此是否存在于组件中并正确渲染 我尝试使用容器使用@testing library/jest native中的toHaveStyle,返回错误toHaveStyle不是函数。然后我用queryByTestId尝试了同样的方法,同样的错误。当我做expect时(getByTestId('background').toBeI

在我新的React原生应用程序中,我想添加一些Jest测试

一个组件渲染背景图像,该图像直接位于项目的
assets
文件夹中

现在,我不知道如何测试该图像是否确实是从该路径获取的,因此是否存在于组件中并正确渲染

我尝试使用容器使用
@testing library/jest native
中的
toHaveStyle
,返回错误
toHaveStyle
不是函数。然后我用
queryByTestId
尝试了同样的方法,同样的错误。当我做
expect时(getByTestId('background').toBeInTheDocument)然后我觉得这是无用的,因为它只检查是否存在具有此testId的元素,而不检查图像源

请问,我如何测试这个?测试一个图像源到底有意义吗

这是我的密码:

1.)应测试的组件(
背景
):

const-Background:React.FC=()=>{
const image=require('../../../../assets/images/image.jpg');
返回(
);
};
2.)试验:

从“React”导入React;
从“反应本机测试库”导入{render,container};
从'@testing library/jest native'导入{toHaveStyle};
导入“@testing library/jest native/extend expect”;
从“../Background”导入背景;
描述('Background',()=>{
测试('渲染背景图像',()=>{
const{getByTestId}=render();
expect(getByTestId('background')。toBeInTheDocument);
/*const container=render();
expect(container).toHaveStyle(
`背景图像:url('../../../../assets/images/image.jpg')`,
); */
/*expect(getByTestId('background')).toHaveStyle(
`背景图像:url('../../../../assets/images/image.jpg')`,
); */
});
});

这有点难说,因为我们看不到
组件或它的功能。。。但是如果它的工作原理类似于
如果您使用的是
@testing library/react
而不是
@testing library/react native
,并且图像上有
alt
属性,那么您可以避免使用
getByDataTestId
而改用
getByAltText

it('使用正确的src',async()=>{
const{getByAltText}=await render();
const image=getByAltText('the_alt_text');
expect(image.src).toContain('the_url');
//或
expect(image).toHaveAttribute('src','the_url'))
});


不幸的是,似乎是这样。(谢谢,!)

当我使用
getByAltText
getByDataTestId
时,我得到的
不是一个函数
错误

因此,对我起作用的是:

const imgSource = require('../../../../assets/images/image.jpg');
const { queryByTestId } = render(<MyComponent testID='icon' source={imgSource}/>);
expect(queryByTestId('icon').props.source).toBe(imgSource);

const imgSource=require('../../../../assets/images/image.jpg');
const{queryByTestId}=render();
expect(queryByTestId('icon').props.source).toBe(imgSource);

我使用
@testing library/react native:“^7.1.0

我今天遇到了这个问题,发现如果您的URI是URL而不是必需的文件,那么将源
URI
缝合到
testID
上效果很好

export const imageID='image_id';
...
试验
导入{
图像ID
},由“.”起;
...
常量testURI=https://picsum.photos/200';
const{getByTestId}=render();
expect(getByTestId()).toBeTruthy();

我得到了
getByDataTestId不是一个函数
我想他们在
@testing library/react native中删除了它“^7.1.0
image.getAttribute('src')
你也可以使用
expect(image)。toHaveAttribute('src','u url')
)这应该是正确的答案。依我拙见