开发者

Proper way of interacting with a RESTful web service using Cucumber

I'm using Cucumber to test end to end application behavior in my Rails-based web service. I currently have a Scenario outline that looks like the following (making up a hypothetical scenario here of creating a user with another user):

  Scenario Outline: Create a user with another user
    Given I want to create a user as a user "<user>"
    When I create a user with name "<name>"
    And the user's age is "<age>"
    Then then the response should be "<res开发者_JAVA百科ponse>"

Scenarios: Create user with 3 args
  | user  | name            | age              | response                    |
  | bob   |  joe            | 25               | <some_xml_response>         |

I'm having a bit of difficulty figuring out how I should write the step definitions for this outline. Basically I'm currently concatenating an XML blob (for name+age) and need to do something similar to how rspec uses :post to post to a controller and see a response. My step definitions currently look like:

Given /^I want to create a user with another user "([^"]*)"$/ do |user|
  @reqxml << "<user><creator>#{user}</creator>"
end

When /^I create a user with name "([^"]*)"$/ do |name|
  @reqxml << "<name>#{name}</name>
end

And /^the user's age is "([^"]*)"$/ do |age|
  @reqxml << "<age>#{age}</age>"
end

Then /^then the response should be "([^"]*)"$/ do |response|
 # ?? not sure if I can use rspec :post here?
end

What's the best way to improve this Cucumber outline? I have a lot more I want to test. In rspec this is rather straight forward and maybe the right answer is to stick this in RSpec. But I really want to get better use out of Cucumber and have a better "bigger" picture with end to end user story testing such as the one above.


I use capybara/rspec/cucumber and pickle in concert to minimize the actual amount of step definitions I have to write, and generally it gets the job done.

Pickle makes steps such as "Given a model exists with" automatically available, capybara's web steps take care of the browser automation like "goto route" or and also provides css and xpath functions to verify the output with steps such as "should contain" etc....

This way you can test your whole stack, which is kind of the point of cucumber

My personal opinion is that cucumber is a domain specific language and as such, may not always be able to describe what you are trying to do in a concise fashion. (think complex object relationships). If you have non-technical users that need to read your tests then its pretty good, but it doesn't beat plain rspec otherwise.

Your cucumber steps are a bit askew too. "Given" describes the pre-existing conditions, not you goals. "When" should describe the actions you take to create a user, and "Then" should verify that the actions taken effected your goal.


While I have seen this being done, testing APIs with cucumber brings with it a special kind of pain.

If your API is truly restful, writing "controller" specs for the integration should actually be fine. Or, if you really want "proper" full-stack integration testing: Do yourself a favor and use capybara/rspec instead of Cucumber...

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜