如何避免黄瓜特征中的硬编码值?

cod*_*ing 1 ruby-on-rails cucumber

在编写我的场景时,是否有可能不必在步骤中硬编码文本?

比如说我在文本框字段中插入用户名,在密码字段中插入密码.

如果我需要在很多地方这样做,那么修复它会很痛苦.

例:

Given I am the registered member "myusername"
And I am on the login page
When I fill in "email" with "email@example.com"
And I fill in "password" with "123"
And I press "Login"
Then I should see "Account Activity"
Run Code Online (Sandbox Code Playgroud)

我不希望我的用户名,电子邮件和密码硬编码.

Rya*_*igg 7

好的,您仍在使用cucumber-rails默认安装的训练轮附带的旧版宝石.阅读AslakHellesøy撰写的这篇文章"训练轮脱落".

这篇文章的主旨是使用web_steps.rb,虽然多年来一直是"标准",现在非常错误,我们应该为此做坏事.

Cucumber的目的是使用它为所有人提供可读/可理解的功能.

编写这样的场景既漫长又无聊:

And I am on the login page
When I fill in "email" with "email@example.com"
And I fill in "password" with "123"
And I press "Login"
Then I should see "Account Activity"
Run Code Online (Sandbox Code Playgroud)

您想要实际测试的是您应该能够登录并在此之后看到与登录有关的事情.无论什么东西都不应该写在场景中.

理想情况下,您Scenario(以更激动人心的方式)看起来像这样:

When I login successfully
Then I should see that I am logged in
Run Code Online (Sandbox Code Playgroud)

然后,做腿部工作的任务转到一些新的步骤定义.这两个步骤并不是自动为您定义的web_steps.rb,而是需要将它们写入文件中feature/step_definitions.你称之为文件取决于你,但它包含的内容与此类似:

When /I login successfully/ do
  visit root_path
  click_link "Login"
  fill_in "Email", :with => "you@example.com"
  fill_in "Password", :with => "password"
end

Then /^I should see I am logged in$/ do
  page.should have_content("Account Activity")
end 
Run Code Online (Sandbox Code Playgroud)

没有更多的web_steps.rb文件和更清晰的步骤定义.究竟黄瓜应该是什么.