【发布时间】:2017-02-28 00:20:48
【问题描述】:
我正在为 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>
编辑:我确实检查了这个资源,但它没有帮助我:stackoverflow.com/questions/11483967/… 我试过choose('Manhattan')。但是,我不断收到此错误:Failure/Error: choose('Manhattan') Capybara::ElementNotFound: Unable to find radio button "Manhattan"
好消息是 rspec 至少在寻找单选按钮。
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')
【问题讨论】:
-
@Joel B,请在您的问题中添加您上一条评论的详细信息,这将对我们将来有所帮助。
-
您确定 Capybara 正在查看正确的网址吗?你是在choose方法之前使用Capybara的visit方法,还是类似的?
-
看起来你做对了。当您在浏览器中转到“报告/新建”时,打开一个 JavaScript 控制台并执行 $('#borough_3');你应该找到它。值得仔细检查吗?
-
choose 会按名称或 id 查找,没关系。
标签: ruby-on-rails ruby rspec tdd sinatra