Rails Capybara 功能测试断言 page.has_content?失败
Rails Capybara Feature Tests with assert page.has_content? fails
我正在尝试使用 Capybara 为我的 rails 应用程序实施一些功能测试。
现在我卡住了。
我的 html 中有一个元素如下所示:
<p class="no-stars">
<strong class="">No stars:</strong>0
</p>
我的测试是这样的:
test "should increase the number of stars for a user" do
Capybara.current_driver = Capybara.javascript_driver
visit users_path
click_link('John F. Kennedy')
assert page.has_content?('No stars: 0'), 'no matching content found!'
page.find("input[value='+1']").click
assert page.has_content?('No stars: 1'), 'no matching content found!'
end
在我看来,我的测试失败是因为找不到内容 'No stars: 0'。
有人能解决这个问题吗?
在你的 html 中,在 : 和 0 之间没有 space,但在你的测试中有一个 space 会导致失败。
顺便说一句,Capybara 提供了一些断言(assert_text、assert_selector、assert_no_text、...),可以让事情变得更好看,并提供更详细的错误消息。而不是断言 page.has_content... 你可以做
page.assert_text('No stars:0')
我正在尝试使用 Capybara 为我的 rails 应用程序实施一些功能测试。
现在我卡住了。
我的 html 中有一个元素如下所示:
<p class="no-stars">
<strong class="">No stars:</strong>0
</p>
我的测试是这样的:
test "should increase the number of stars for a user" do
Capybara.current_driver = Capybara.javascript_driver
visit users_path
click_link('John F. Kennedy')
assert page.has_content?('No stars: 0'), 'no matching content found!'
page.find("input[value='+1']").click
assert page.has_content?('No stars: 1'), 'no matching content found!'
end
在我看来,我的测试失败是因为找不到内容 'No stars: 0'。
有人能解决这个问题吗?
在你的 html 中,在 : 和 0 之间没有 space,但在你的测试中有一个 space 会导致失败。
顺便说一句,Capybara 提供了一些断言(assert_text、assert_selector、assert_no_text、...),可以让事情变得更好看,并提供更详细的错误消息。而不是断言 page.has_content... 你可以做
page.assert_text('No stars:0')