开发者

Correct way to test scenarios with cucumber (rails 2.3.8)

So, I am starting to build a new application, my first big one in rails since moving from .net.

I really want to BDD the entire application so I have cucumber all set up and ready to go.

I have written more then a couple of test for the simple stuff (deleting, adding, updating) and so on but now I am stuck trying to figure out how to test this scenario.

I have these models User, Account, Plan each account has a plan, the plans are billable each month (on the same day the account was created).

I want to test selecting a plan, creating an account, check the billing process (even mock, without PayPal at this point).

I would appreciate help with this, just to emphasize, I am not looking for complete code, just an explanation of how (in concept) would you go about and test this.

Also, plan is upgradable and downgrade-able so I want to test this as well.

开发者_如何学编程

Thanks in advance for your help


I like to practice outside-in development, where we begin by writing acceptance tests, then drop into unit tests to handle domain logic. Let's take creating an account as an example.

Start by writing a Cucumber story for the desired feature. For example:

Feature: Create an account
  In order to use the application
  As a user
  I want to create an account

  Scenario: Create an account from home page
    Given I am on the home page
    When I follow "Sign up"
    And I fill in "Username" with "bob"
    And I fill in "Password" with "test123"
    And I press "Create"
    Then I should see "You have successfully signed up! You may now sign in."

When we run our Cucumber features using the cucumber features command, the first step in the scenario will fail because the home page does not yet exist. To create it, we may consider it a separate feature. Therefore, we might write another Cucumber feature like:

  Scenario: Visitor visits the home page
    When I go to the home page
    Then I should see "Welcome to the Website of Awesomeness"

Running this feature, we will discover that there is no root route defined in the Rails app. Once we fix that issue, we'll need a controller, view, and the text in the view. So far, we have only written Cucumber tests.

Once all of these features are passing, we realize a username should be required. We can write a Cucumber step to test this case:

  Scenario: Username must be filled out
    Given I am on the home page
    When I follow "Sign up"
    And I fill in "Password" with "test123"
    And I press "Create"
    Then I should see "Username cannot be blank."

To implement this, we must add a validation to our model that will validate the inclusion of a username. Now we will drop to unit testing because we are modifying domain logic. As a general rule, when you modify a model, you should drop into RSpec or Test::Unit and test that modification directly. For example, using RSpec we would add a spec to ensure usernames must be present (and unique, etc). Once this test passes, our scenario should also begin to pass.

This was long-winded, but it should help you start practicing BDD in a very real way. For more information, see the RSpec book (which includes a wealth of information about outside-in practices using Cucumber and RSpec): http://www.pragprog.com/titles/achbd/the-rspec-book

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜