Rspec:选择Sinatra表单的单选按钮?

时间:2016-10-18 16:07:32

标签: ruby-on-rails ruby rspec tdd sinatra

我正在为Sinatra应用编写测试规范。如何让rspec点击表格中的单选按钮?



<form action="/reports/new" method="POST">
<div class="form-group">
    <label class="col-md-4 control-label" for="radios">Borough</label>
      <div>
          <label>
            <input name="Brooklyn" id="borough_1" value="1" type="radio">Brooklyn</input>
          </label>
          <label>
            <input name="Bronx" id="borough_2" value="2" type="radio">Bronx</input>
          </label>
          <label>
            <input name="Manhattan" id="borough_3" value="3" type="radio">Manhattan</input>
          </label>
          <label>
            <input name="Queens" id="borough_4" value="4" type="radio">Queens</input>
          </label>
          <label>
            <input name="Staten Island" id="borough_5" value="5" type="radio">Staten Island</input>
          </label>
        <button value="Submit" type="submit">Submit</button>
      </div>
  </div>
</form>
&#13;
&#13;
&#13;

编辑:我确实查看了这个资源,但它没有帮助我:stackoverflow.com/questions/11483967/…我尝试了choose('Manhattan')。但是,我不断收到此错误:Failure/Error: choose('Manhattan') Capybara::ElementNotFound: Unable to find radio button "Manhattan"

好消息是,rspec至少在寻找一个单选按钮。

&#13;
&#13;
        visit '/reports/new'
        fill_in(:title, :with => "Ben and Jerries Ice Cream")
        fill_in(:business, :with => "Starbucks")
        fill_in(:location, :with => "146 Rikers Street")
        fill_in(:content, :with => "Some great food")
        fill_in(:date, :with => "2016-09-12")
        choose('Manhattan')
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:1)

choose通过 id 名称标签查找内容。来自文档:

  

选择([locator],options)⇒对象

     

找到一个单选按钮并将其标记为已选中。单选按钮可以是   通过姓名,身份证或标签文本找到。

确保:a)Capybara将“报告/新”和b)您的页面按预期呈现这些元素。例如,尝试自己去“reports / new”并在控制台中选择该元素(假设你有jQuery):

$('#borough_3');

或者只是在源代码中查找。