Ruby 为什么我能';找不到h1标签?

Ruby 为什么我能';找不到h1标签?,ruby,rspec,capybara,Ruby,Rspec,Capybara,这是我的测试: 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) HomeC

这是我的测试:

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'
1)HomeController应渲染主页
故障/错误:查找('H1')。应包含内容(“在线模拟电路”)
Capybara::ElementNotFound:
找不到css“H1”
#./spec/controllers/home\u controller\u spec.rb:9:in'block(2层)in'
#(eval):6:in'block in fork'

如何查找标记、ID或CSS???

您不能对从请求方法(如
get
post
)获得的响应使用水豚匹配器。对于水豚匹配器,您必须使用
visit
,如
访问“主页”

有关详细信息,请参阅。这句话特别是:

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

另见本帖:


如果你想测试HTML字符串响应的结构,你可以考虑.< /p>但是如何访问一个没有硬编码的动作?你可以使用例如<代码>访问HOMYOPATION/CODE >,假设在你的路线中定义了代码> HOMYOPATION<代码>。