Reputation: 57
For several of my cuke scenarios, I want to POST the user to a page with an object in the params, so that Rails will make a new object in the database. Is there a convenient way to send a POST+params to an action (either using something in cucumber or rails) or do I have to write my own function using Net::Http?
Upvotes: 2
Views: 3857
Reputation: 711
Just visit the controller you want to post some parameters to in order to create an object.
You do not need a form to do this. Just post the values to whatever controller is handling these posts and continue on with your testing. All the controller cares about is getting params to work with, however they get posted.
visit "/user", :post, :firstName => "Foo", :lastName => "Bar"
As detailed in the webrat documentation:
visit (url = nil, http_method = :get, data = {})
UPDATE: Unless I'm mistaken Capybara has more or less superseded Webrat and generally the there's an opinion that cucumber level tests should exercise the actual user interface, and not the API that interface interacts with. That said, it is still possible and I still find it useful myself when early on in a project I just want to test my controllers and figure out the UI later.
So in Capybara, depending on your driver, but assuming rack/test:
page.driver.post('/user', { firstName: "Foo", lastName: "Bar" })
Upvotes: 5
Reputation: 37133
It sounds like you don't have a form for this action? If you do have a form, you just complete the interaction via the web UI (using the tool of your choice ... webrat etc etc)
Otherwise, if this is to setup some test data, it's probably better to create the object directly in a step.
Given that an object exists
Do ...
Under the "Given" step, create the necessary object too meet the assumption in the rest of the test steps.
Upvotes: 0