【问题标题】:Why I can't find h1 tag?为什么我找不到 h1 标签?
【发布时间】:2013-01-25 09:16:04
【问题描述】:

这是我的测试:

require "rspec"

describe HomeController do
  render_views

  it "should renders the home" do
    get :home
    response.should render_template("home")
    find('H1').should have_content("Simulate Circuits Online")

  end

end

但我明白了:

1) HomeController should renders the home
     Failure/Error: find('H1').should have_content("Simulate Circuits Online")
     Capybara::ElementNotFound:
       Unable to find css "H1"
     # ./spec/controllers/home_controller_spec.rb:9:in `block (2 levels) in <top (required)>'
     # (eval):6:in `block in fork'

如何找到标签、ID 或 CSS ??

【问题讨论】:

    标签: ruby rspec capybara


    【解决方案1】:

    您不能对从getpost 等请求方法获得的响应使用水豚匹配器。对于水豚匹配器,您必须使用visit,如visit 'home'

    详情请参阅this post。尤其是这句话:

    Capybara 作为一个验收测试框架,不会暴露请求或响应对象等低级细节。为了使用 Capybara 访问网页,开发人员需要使用方法 visit(而不是 get)。要读取访问的页面正文,开发人员必须使用页面而不是操作响应。

    另请参阅此帖子:Check page response on multiple pages with Capybara

    如果你想测试一个 HTML 字符串响应的结构,你可以考虑rspec-html-matchers

    【讨论】:

    • 但是如何在不硬编码的情况下访问一个动作呢?
    • 您可以使用例如visit home_path,假设 home_path 已在您的路线中定义。
    猜你喜欢
    • 1970-01-01
    • 2022-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-09
    • 2022-01-21
    • 2012-05-13
    • 1970-01-01
    相关资源
    最近更新 更多