【问题标题】:How to test incrementing numerical id in rspec如何在rspec中测试递增的数字ID
【发布时间】:2013-08-16 12:18:23
【问题描述】:

我有一个复选框表单部分选择全部、部分或一个类:

<p>
  <%= check_box_tag(:all) %>
  <%= label_tag(:all, "All subjects") %>
</p>
<p>
  <% @subjects.each do |subject| %>
    <%= check_box_tag(subject.id) %>  
  <%= label_tag(subject.id, subject.name) %>
  <br />
  <% end %>
</p>

生成以下 HTML:

<p>
  <input id="all" name="all" type="checkbox" value="1" />
  <label for="all">All subjects</label>
</p>
<p>
      <input id="1" name="1" type="checkbox" value="1" />  
    <label for="1">English</label>
    <br />
      <input id="2" name="2" type="checkbox" value="1" />  
    <label for="2">Basket_Weaving</label>
    <br />
      <input id="3" name="3" type="checkbox" value="1" />  
    <label for="3">Math</label>
    <br />
</p>

我想使用 rspec 测试每个主题是否显示在视图中 -

it "should have a box for each subject" do
  @eval_selektor
  @student_group.subjects.each do |subject|
    response.should ??????
  end
end

我尝试过response.should have_selector('id' content: subject.id) but that throwsundefined 方法include?' for 1:Fixnum(除了没有多大意义),我也尝试过xpath/css,如here 所示。

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 rspec


    【解决方案1】:

    你试过了吗:

    find("input[id='#{subject.id}']")
    

    【讨论】:

    • 这给了我NoMethodError: undefined method find' for #<:core::examplegroup::nested_1::nested_1:0x0000010471f098>`
    • 你的规范在哪里?
    • spec/controllers/evaluations_controller_spec - 我应该注意,也许我没有正确使用“查找”。我查看了其他一些关于 SO 的示例,但似乎没有一个与我正在做的事情真正匹配
    • find 应该仅用于请求/功能规范
    • 啊,好吧,那是有道理的:p 我应该在控制器规范中使用什么?
    猜你喜欢
    • 2012-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-07
    • 1970-01-01
    • 2022-09-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多