我的测试代码有什么问题?

时间:2012-02-04 14:19:16

标签: cucumber capybara

我有像这样的步骤定义黄瓜

find("div#cycle_form table").should have_content(append_row_to_table_tag("day_num", "hour", "hour_night"))

方法append_row_to_table_tag返回字符串

"<tr><td>data</td><td>data</td><td>data</td><td>data</td><td>data</td></tr>"

并查看

%div{:id => "cycle_form", :class=>"list_form"}
  = link_to("Add",nil,:id=>"create_row_cycle")#,:onclick=>"$('div#cycle_form table').append('#{append_row_to_table_tag("day_num", "hour", "hour_night")}');return false;", :href=>"")
  %table
    %th day
    %th hour
    %th hour_night
    %th edit_button
    %th delete_button

黄瓜失败

expected there to be content "<tr><td>data</td><td>data</td><td>data</td><td>data</td> 
<td>data</td></tr>" in "day hour hour_night edit_button delete_button\ndata data data 
data data" (RSpec::Expectations::ExpectationNotMetError)

如何使用带有capybara的JavaScript测试向页面添加元素?

2 个答案:

答案 0 :(得分:0)

您可以使用像Jasmine这样的宝石来测试Javascript。 Here是一个很好的截屏视频,展示了它是如何运作的。

答案 1 :(得分:0)

Capybara的have_content方法处理文本内容 - 而不是HTML标记。一般来说,Capybara在DOM之上运行,所以你最好使用它的选择器来查询你想要断言的表的各个元素。

如果你想将Cucumber表的内容与HTML表进行比较,我发现了a nice example

如果您真的想测试页面上是否包含特定标记,我相信您可以下载到Nokogiri的本机表示并以这种方式访问​​HTML,或者甚至断言page.body以获取完整的HTML源代码,但我认为这两者都是最后的手段。