0

我对此有点陌生,但我试图让 rspec 点击链接。我已将索引寻呼机上的“显示”链接替换为项目名称的链接。

即我的观点有这个

     <td><%= link_to task.name, task_path(task) %></td>

我的 rspec 文件有一个参考

    find(:xpath, "(//a[text()= 'Show'])[1]").click

我想变成类似的东西

    find(:xpath, "(//a[text()= @task1.name])[1]").click

那应该在列表中选择正确的任务,以便我可以按照它

    it { should have_content(@client.name) }
    it { should have_content(@task1.name) }
    it { should have_content(@task1.description) }

不幸的是,我无法弄清楚如何将 @task.name 放入其中以使其不会损坏。

有任何想法吗?

更新:

    Failure/Error: find(:xpath, "(//a[text()= @task1.name])[1]").click
    Capybara::ElementNotFound:
    Unable to find xpath "(//a[text()= @task1.name])[1]"
    # ./spec/requests/task_pages_spec.rb:123:in `block (4 levels) in <top (required)>'
4

2 回答 2

0

请尝试xpath这种方式,您错过了 Ruby 的字符串插值

find(:xpath, "//a[text()= \"#{@task1.name}\"][1]").click

我试图制作一个类似的例子:-

require 'nokogiri'

doc = Nokogiri::HTML::Document.parse <<-eotl
<div>
  <p>foo</p>
  <p>foo</p>
  <p>bar</p>
</div>
eotl

doc.class # => Nokogiri::HTML::Document

class Person
  attr_accessor :name
end

ram = Person.new
ram.name="foo"
ram.name # => "foo"
doc.search("//div/p[text()= \"#{ram.name}\"]").to_a.size
# => 2
于 2013-09-02T18:11:27.160 回答
0

这比我想象的要容易得多......

    click_link @task1.name

我想我可能需要在角落里哭一会儿,花了几个小时试图解决这个问题……

于 2013-09-03T11:17:06.107 回答