How do you POST to a URL in Capybara?

前端 未结 8 675
无人及你
无人及你 2020-11-28 09:51

Just switched from Cucumber+Webrat to Cucumber+Capybara and I am wondering how you can POST content to a URL in Capybara.

In Cucumber+Webrat I was able to have a ste

8条回答
  •  一个人的身影
    2020-11-28 10:13

    I know the answer has already been accepted, but I'd like to provide an updated answer. Here is a technique from Anthony Eden and Corey Haines which passes Rack::Test to Cucumber's World object:

    Testing REST APIs with Cucumber and Rack::Test

    With this technique, I was able to directly send post requests within step definitions. While writing the step definitions, it was extremely helpful to learn the Rack::Test API from it's own specs.

    # feature
      Scenario: create resource from one time request
        Given I am an admin
        When I make an authenticated request for a new resource
        Then I am redirected  
        And I see the message "Resource successfully created" 
    
    # step definitions using Rack::Test
    When /^I make an authenticated request for a new resource$/ do
      post resources_path, :auth_token => @admin.authentication_token
      follow_redirect!
    end
    
    Then /^I am redirected$/ do
      last_response.should_not be_redirect
      last_request.env["HTTP_REFERER"].should include(resources_path)
    end
    
    Then /^I see the message "([^"]*)"$/ do |msg|
      last_response.body.should include(msg)
    end
    

提交回复
热议问题