英文:
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 "Version"
The steps are defined as follows:
Given 'I am on the About page' do
visit "/about"
end
Then 'I should see {string}' do |page_text|
page.has_text?(page_text)
end
The page itself is defined in about.html.erb and has the following content
<h1>StaticPages#about</h1>
<p>Find me in app/views/static_pages/about.html.erb</p>
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)
只是一个返回 true
或 false
的谓词,相反,你需要设置一个在失败时引发错误的断言。你没有指明你是否在使用 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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论