开发者

How do I use Capybara get, show, post, put in controller tests?

I'm just not sure what the exact way to word it is..

This tells me 'No response yet. Request a page first.'

it "should list searches" do
  get 'index'
  page.should ha开发者_开发技巧ve_selector('tr#search-1')
end

Is it meant to be like this instead?

it "should list searches" do
  get 'index' do
    page.should have_selector('tr#search-1')
  end
end

That way doesn't seem to actually test anything though.

What's the correct way?


According to this, Capybara doesn't support PUT and DELETE requests with the default driver. PUT and DELETE are usually faked with javascript to accomodate REST based architecture.

I haven't checked, but I believe that you can use PUT and DELETE with Capybara if you make it use one of it's JS compatible drivers such as Selenium.


The get, post, etc. methods are part of the functional test interface for Rails controller tests, which is a completely separate testing system. You cannot use them with Capybara.

For GET requests, use Capybara's visit method instead.

For other request types (POST, PUT, ...), either do what a user does and visit a page with a form to fill out and submit. Or, if you are testing an API, write a functional test for your controller without using Capybara, like so:

post :index
response.status.should == 200
response.body.should contain('Hello World')

See also Jonas's post on why you shouldn't test APIs with Capybara.


You usually use visit with Capybara to visit pages, I think.

it 'should list searches' do
  visit '/'
  page.should have_selector('tr#search-1')
end
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜