Working outside-in with Cucumber blurs the lines between testing and development. Always be ready to learn something new about the problem domain, whether you’re deciding on the wording in a Cucumber scenario or choosing the parameters for a method. By taking care to craft a clean interface between your tests and the system underneath, you’ll end up with tests that can easily evolve with the system’s changing requirements.
In this chapter, we showed you how to use Cucumber to trap a bug. In Chapter 13, Adding Tests to a Legacy Application, we’ll talk much more about this.
We covered the basics of creating Sinatra applications and testing them using Capybara. There’s a whole chapter specifically on using Capybara, Chapter 15, Using Capybara to Test Ajax Web Applications. Sinatra is a useful tool for testers to know about. Sometimes, when the system you’re testing depends on an external web service that you can’t control, you’ll want to create a test double of that external service. Sinatra is a quick and easy way of creating such a test double.
We also showed you how to use Cucumber’s hooks to invoke Ruby code before and after each scenario or to run them before specific scenarios using tags.
Next we’ll introduce a new problem by changing the architecture to make the system asynchronous.
Here are some exercises for you to try for yourself.
Our user experience aficionados have had a rethink. Early reports from user testing are that most users don’t want to have to type in a precise amount of money; they want to click a button with a fixed amount. Without changing the Cucumber scenario, can you change the support code and then the user interface itself so that the user has to click only a single button to get their $20?
Can you add some more scenarios (and some more buttons) for other fixed amounts?
We need to help our customers not to overdraw. Add a new scenario that looks like this:
| Feature: Prevent users from going overdrawn |
| |
| Scenario: User tries to withdraw more than their balance |
| Given my account has been credited with $100 |
| When I withdraw $200 |
| Then nothing should be dispensed |
| And I should be told that I have insufficient funds in my account |
Can you implement it?
Our users would like to be able to check their balance from the ATM. Decide how you’d like the user interface to be—perhaps you’ll show a menu from which they can choose to make a withdrawal or check their balance—and then implement this scenario:
| Feature: Display balance |
| |
| Scenario: User checks the balance of an account in credit |
| Given my account has been credited with $100 |
| When I check my balance |
| Then I should see that my balance is $100 |
For instructions on installing Bundler, see Appendix 2, Installing Cucumber.
http://cukes.info/cucumber/api/ruby/latest/Cucumber/RunningTestCase/Scenario
http://cukes.info/cucumber/api/ruby/latest/Cucumber/RbSupport/RbDsl.html
18.226.34.197