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

拟墨画扇 提交于 2019-12-23 18:25:22

问题


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 have_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?


回答1:


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.




回答2:


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.




回答3:


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


来源:https://stackoverflow.com/questions/5908216/how-do-i-use-capybara-get-show-post-put-in-controller-tests

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!