黄瓜测试 - 选择哪种语法来测试单选按钮选择



我现在对编码和学习Ruby和Sinatra相当陌生。我已经开始为我尝试导出到网络的终端开发石头剪刀布游戏,并使用黄瓜来测试其网络功能。

我有一个与 erb 文件集成的 Web 功能,有趣的是它在 Web 中工作,但我找不到实际使其通过并显示它有效的黄瓜 Web 步骤/语法。我想以正确的方式编码,所以希望在我继续新功能之前我的测试实际工作。

我的 options.erb 文件有以下几行:

<form action="game_play">
  <fieldset>
    Which game do you want to play?<br><br>
    <input type="radio" name="type" value="RPS" checked>Rock Paper Scissors<br>
    <input type="radio" name="type" value="RPSSL">Rock Paper Scissors Spock Lizard
    <br><br>
    <input type="submit" value="PLAY!">
  </fieldset>
</form>

我的Sinatra服务器包括以下内容

get '/game_play' do
  @type = params[:type]
  erb :play
end

在play.erb页面中,石头,纸和剪刀会自动显示,但只有这个if语句

<% if @type == "RPSSL" %>

允许斯波克和蜥蜴出现。在网络上,如果我在 localhost:9292 上累积并访问,它可以工作;只有选择RPSSL才能在下一页上看到斯波克和蜥蜴。

这些黄瓜测试

Scenario: Choosing a RPS type of game
  Given I am on the options page
  When I check "Rock Paper Scissors" within "type"
  And I press "PLAY!"
  Then I should see "Rock"
  But I should not see "Spock"
Scenario: Choosing a RPSSL type of game
  Given I am on the options page
  When I check "Rock Paper Scissors Spock Lizard" within "type"
  And I press "PLAY!"
  Then I should see "Rock"
  And I should see "Spock"

不工作。第一个实际上通过了,因为默认情况下会检查 RPS,但检查可能不是正确的黄瓜语法。我尝试使用选择,选择,甚至填写,但尽管在网上花了很长时间,但找不到单选按钮的正确黄瓜语法。有人能帮忙吗?谢谢。

好的,所以有一件事是你应该真正删除 websteps 并编写自己的 websteps,但让我们看一下支持签入 websteps 的代码:

https://github.com/GBouffard/rps-challenge/blob/master/features/step_definitions/web_steps.rb#L76

When /^(?:|I )check "([^"]*)"(?: within "([^"]*)")?$/ do |field, selector|
  with_scope(selector) do
    check(field)
  end
end

您也应该真正向我们展示您得到的实际错误,但我怀疑从您的黄瓜步骤When I check "Rock Paper Scissors Spock Lizard" within "type"实际复选框标签中存在一些错误标签。

这是一种我会用byebug攻击的问题,以检查是否正在抓取正确的HTML元素......

好的,所以进入那里的byebug并深入研究水豚来源,我想我已经修复了它。 如果看一下水豚测试如何使用"单选按钮",这就是你在这里使用的,你需要这样的东西

<td><input type="radio" name="type" value="RPS" id="RPS" class="regular-checkbox" checked />
<label for="RPS">Rock Paper Scissors</label><br><br><img src="/images/rps_button.jpg"></td>
<td><input type="radio" name="type" value="RPSSL" id="RPSSL" class="regular-checkbox"/>
<label for="RPSSL">Rock Paper Scissors Lizard Spock<br><br><img src="/images/rpssl_button.jpg"></td>   

您需要按如下方式更新您的方案,以确保您"选择"单选按钮"而不是"检查它"

  Scenario: Choosing a RPS type of game
    Given I am on the homepage
    When I fill in "name" with "Guillaume"
    And I press "START"
    And I choose "Rock Paper Scissors"
    And I press "PLAY!"
    Then I should see "Rock"
    But I should not see "Spock"
  Scenario: Choosing a RPSSL type of game
    Given I am on the options page
    When I choose "Rock Paper Scissors Spock Lizard"
    And I press "PLAY!"
    Then I should see "Rock"
    And I should see "Spock"

最新更新