Rails Cucumber Tests Getting Green on Red

huangapple go评论60阅读模式
英文:

Rails Cucumber Tests Getting Green on Red

问题

以下是您要翻译的内容:

我有一组相当简单的Cucumber测试,用于基本的Rails应用程序,尽管我希望它们失败,但它们将会通过。这些测试只是导航到一个静态页面,并测试页面上特定文本的存在。

其中一个Cucumber测试如下所示:

Scenario: 访问关于页面
  Given 我在关于页面上
  Then 我应该看到 "版本"

步骤定义如下:

Given '我在关于页面上' do
  visit "/about"
end

Then '我应该看到 {string}' do |page_text|
  page.has_text?(page_text)
end

页面本身在about.html.erb中定义,内容如下:

<h1>StaticPages#about</h1>
<p>在 app/views/static_pages/about.html.erb 中找到我</p>

这个测试显然应该失败,因为它不包含文本 "版本",但它通过了。我如何查明测试为什么通过?调试这些问题的最佳方法是什么?

英文:

I have a rather trivial set of Cucumber tests for a basic Rails application that will pass, despite the fact that I expect them to fail. The tests simply navigate to a static page and test for the presence of specific text on the page.

One of the Cucumber tests is as follows below:

Scenario: Visit the About screen
  Given I am on the About page
  Then I should see &quot;Version&quot;

The steps are defined as follows:

Given &#39;I am on the About page&#39; do
  visit &quot;/about&quot;
end

Then &#39;I should see {string}&#39; do |page_text|
  page.has_text?(page_text)
end

The page itself is defined in about.html.erb and has the following content

&lt;h1&gt;StaticPages#about&lt;/h1&gt;
&lt;p&gt;Find me in app/views/static_pages/about.html.erb&lt;/p&gt;

This test should clearly fail since it does not contain the text "Version", but it passes. How can I tell why the test is passing? What is the best approach for debugging these kinds of issues?

答案1

得分: 1

通过是因为你实际上没有测试任何东西。page.has_text?(page_text) 只是一个返回 truefalse 的谓词,相反,你需要设置一个在失败时引发错误的断言。你没有指明你是否在使用 RSpec 或 minitest,但类似于

expect(page).to have_text(page_text) # Rspec

或者

assert_text(page_text) # minitest

应该是你要使用的。

英文:

It's passing because you're not actually testing anything. page.has_text?(page_text) is just a predicate which returns true or false, instead you need to set an assertion which will raise an error in the case of failure. You don't indicate whether you are using RSpec or minitest, but something along the lines of

expect(page).to have_text(page_text) # Rspec

or

assert_text(page_text) # minitest

should be what you use

huangapple
  • 本文由 发表于 2023年5月11日 09:29:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/76223553.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定