我正在使用Testing Library为 React 应用程序编写一些测试。我想检查一些文本是否出现,但我需要检查它是否出现在特定位置,因为我知道它已经出现在其他地方。
查询的测试库文档说getByText查询需要一个container参数,我猜可以让您在该容器内进行搜索。我尝试这样做,container并text按照文档中指定的顺序使用和参数:
const container = getByTestId('my-test-id');
expect(getByText(container, 'some text')).toBeTruthy();
Run Code Online (Sandbox Code Playgroud)
我收到一个错误:matcher.test is not a function。
如果我把参数反过来:
const container = getByTestId('my-test-id');
expect(getByText('some text', container)).toBeTruthy();
Run Code Online (Sandbox Code Playgroud)
我得到一个不同的错误: Found multiple elements with the text: some text
这意味着它不在指定的容器内搜索。
我想我不明白是如何getByText工作的。我究竟做错了什么?
我有一个函数,我希望能够向其中传递一个列表列表,就像这个人为的例子一样:
sub print_lists(@input) {
.say for @input
}
my @list_of_two_lists = ((1, 2), (3, 4));
print_lists(@list_of_two_lists);
Run Code Online (Sandbox Code Playgroud)
正如预期的那样,这给出了以下输出:
(1, 2)
(3, 4)
Run Code Online (Sandbox Code Playgroud)
但如果我这样做:
my @list_of_one_list = ((1, 2));
print_lists(@list_of_one_list);
Run Code Online (Sandbox Code Playgroud)
我得到这个输出:
1
2
Run Code Online (Sandbox Code Playgroud)
即,它将包含两个元素的一个列表的列表展平为两个元素的单个列表。
我究竟做错了什么?如果我希望能够传入包含单个列表的列表,我需要做什么?