rails 3.2 capybara Capybara::ElementNotFound:Unabl

2019-05-03 08:36发布

问题:

I am trying to test my rails app with rspec 2.10.0 + capybara 1.1.2. Here is my test file

require 'spec_helper'

    describe AdminPanelController do
      describe "index" do
        it "should have return code 200" do
          visit '/admin'
          page.should have_content "hello"
          #response.status.should be(200)
        end
      end
    end

And here are test result

 Failure/Error: page.should have_content "hello"
 Capybara::ElementNotFound:
   Unable to find xpath "/html"

I google about this issue but find only information that webrat can be a problem however i do not have this gem installed. Thanks for any suggestions.

回答1:

Wrong type of test. This looks like a controller test, which does tests with methods like get and post and is in the spec/controllers folder. Request specs, which use capybara, reside in spec/requests. Run $ rails generate scaffold SomeModel to see how they each should look.

If you understood the above but would still like to use capybara for your controller test, modify your describe block:

describe AdminPanelController, :type => :request do
  ...
end