CSC/ECE 517 Fall 2013/ch1 1w33 aa
Behavior Driven Development in Ruby on Rails
Behavior-driven development (also known as BDD) is an Agile development strategy that has been derived from Test Driven Development(also known as TDD). It was developed by Dan North in order to address the problems faced by TDD.
Introduction
Behavior Driven Development approach focuses on design, documentation and behavior rather than on tests. This approach enables more interactions between the client and the developer. The client specifies his functional specifications to the developer by writing user stories which describe how the system should perform when a certain condition is met. These user stories are then translated into tests using tools like RSpec and Cucumber. These tests are then run to check whether they satisfy the desired functional behavior. The business requirement is delivered when all the tests pass.
Difference from other development strategies
Test-driven development (TDD)
Another type of Test Driven Development is Behavior-driven development (also known as TDD). In TDD, software developers write automated test cases before they write any code.
- • The first step of TDD is to write a failing test for a particular unit of functionality to be added.
- • The next step is to write a piece of code only to pass that failing test.
- • The last step would be to refactor the code so that it meets the requirement standards of the code.
Test Driven Development follows three rules. They are:
- • One must not write any production code unless it is to make a failing unit test pass.
- • One must not write any more of a unit test than is sufficient to fail
- • One must not write any more production code than is sufficient to pass the one failing unit test.
Difference between TDD and BDD
- • TDD’s main focus is on the tests while BDD mainly focuses on the behavioral specification from the user point of view.
- • With TDD, unit tests are written, whereas for BDD tests are written using “User Stories”.
- • TDD uses language which is intelligible to only the software programmers.
- • BDD uses language which is more easily understood by technical as well as non technical people.
Advantages of BDD
- • Due to BDD language being comprehensible by both technical as well as domain experts, the BDD involves the customers and the business partners in writing the scenarios for the tests.
- • Unlike tests which come after implementation, focusing on the behavior proves to be a better approach.
- • TDD methodology can lead to a number of failed tests before arriving at the correct result because it focuses mainly on implementation. But BDD does not require so many tests as its main focus is behavior thus resulting in less code.
Disadvantages of BDD
- • The user stories written by the customers might not be good quality and may have to be rewritten by the developer.
- • The customer might not have enough time to review the scenarios on a regular basis.
Examples of BDD with Ruby on Rails
In BDD, business requirements are identified by user stories. The user stories are described using the following format
As a [role] I can [feature] so that [reason]
Example to explain this-
As a Bank account holder I can login with my credentials so that I can check my account details
Once the user stories are written, the acceptance tests can be written for the user stories. The acceptance tests have the following format
Given [Context] When [Event Occurs] Then [Outcome]
Scenario: Enter a Login Page
Given I enter a valid username And I enter a valid password When I press the Login Button Then I should be redirected to the home page
These acceptance tests for the user stories are entered in the application as automated tests by the developer.
Implementation
Ruby on Rails uses various tools like Cucumber, Rspec etc to implement the user tests using Behaviour-driven development.
Cucumber
BDD makes the use of Cucumber for running automated acceptance tests. Cucumber specifies what a user should see and be able to do. Cucumber works with Ruby to help the software development teams describe how an application should behave. Cucumber is intelligible to both software developers as well as domain experts. It can serve both as documentation and automated tests.
RSpec
RSpec mainly deals with unit tests which mean testing a class method in isolation from the rest of the application. So it guarantees that the model does what it is supposed to do, the controller does what it is supposed to do, etc. It explains the behavior of objects at the code level. It provides a simple and elegant syntax and works effectively with some other frameworks like Rails and Mocha library.
RBehave
RBehave is a framework based on the story framework from JBehave which describes behaviour at the application level. It is simpler than RSpec and eliminates the need to reuse code. The choice of frameworks depend on the level of granularity. RBehave and RSpec are often used as a combination to simplify BDD wordings.
References
1. Behavior-driven development
2. Rules of Test-driven development
4. Difference between TDD and BDD
5. Benefits of behavior-driven development
6. Lifecycle
10. Cucumber
11. Behavior driven development with Rails
13. Difference between RSpec and Cucumber
14. RBehave