开发者

Should all validation rules be tested within a Cucumber feature?

In Ruby on Rails, if all of the validation rules for a given model are being tested within that model's spec (or unit tests), is it still considered necessary to write a Cucumber scenario for each validation?

Would it 开发者_开发技巧suffice to instead write two scenarios: One for when valid data is entered, and one for when invalid data is entered?


This is a good question, and the answer is: It depends.

You can think of Cucumber as a way of communicating between the product owner, developers and testers.

If you feel that having the validations in Cucumber adds to the shared understanding of what the product does, then keep them there.

One approach is to combine the validations into a scenario outline:

Scenario Outline: User tries to register but skips a mandatory field
  Given I am registering
  And I leave the "<field>" blank 
  When I click "Submit"
  Then I should see "<message>"
  And I should not be registered
    | field         | message                         |
    | Forename      | Please enter your forename      |
    | Surname       | Please enter your surname       |
    | Date of Birth | Please enter your date of birth | 


This is a great question, and one that I have been dealing with recently.

It may be different for your organization, but in my organization we try to leave the field validation tests to the unit tests or some other framework that handles these situations better. Aside from that, I am of the school of thought that Cucumber is meant purely for automated acceptance testing (AKA a communications tool between you/your team and the PO) -- and that other methods should be used for anything outside that scope.


Why leave anything open to suggestion? Allowing for assumptions made by developers is inherently risky in terms of generating rework.

How each field a form behaves under different circumstances (create, display, edit, persona) is clearly acceptance criteria.

If you hide it away in a unit test then you are disconnecting it from the living documentation

You really shouldn't need unit tests if you are really bought into the BDD way.

Feature: Edit staff personal
  Scenario Outline: Form validation
    Given I am editing a staff personal details
    And the form contains a "<Mandatory?>" field with a label "<Label>"
    And text fields have a input length of between "<Min Length>" and "<Max Length>"
    And select fields have these "<Options>"
    When I submit the form by clicking the save button
    Then an error displays if validation fails
    But commits my changes if validation is successful and returns the form back to display mode
    Examples:
    | Label | Mandatory? | Type | Min length | Max length | Options
    | Title | true | select | 0 | 0 | Mr, Mrs, Miss, Ms, Dr, Prof |
    | Surname | true | text | 2 | 50 | null |
    | Forename | true | text | 2 | 50 | null |
    | Known as / Other Surname | false | text | 2 | 50 | null |
    | Known as forename | false | text | 2 | 50 | null |
    | Date of birth | true | date-picker | 0 | 0 | null |
    | NI number | true | ni-number | 0 | 0 | null |
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜