{"id":92,"date":"2019-07-08T14:31:27","date_gmt":"2019-07-08T14:31:27","guid":{"rendered":"https:\/\/www.bddtesting.com\/?page_id=92"},"modified":"2019-07-08T14:31:27","modified_gmt":"2019-07-08T14:31:27","slug":"rspec-and-bdd","status":"publish","type":"page","link":"https:\/\/www.bddtesting.com\/rspec-and-bdd\/","title":{"rendered":"RSpec and BDD"},"content":{"rendered":"\n

The Ruby community loves testing. It\u2019s an area that\u2019s under constant evolution and “best practices” are always shifting. But there\u2019s a majority forming around RSpec. It\u2019s a great tool for implementing Test Driven Development (TDD) and Behavior Driven Development (BDD).<\/p>\n\n\n\n

Behavior Driven Development<\/h2>\n\n\n\n

There are generally four tiers to testing practice: test last, test first, test-driven (TDD), and behavior-driven (BDD).<\/p>\n\n\n\n

In test last<\/em> development you write an automated test suite to exercise code that has already been written. This is the most common form of testing (across languages), but in the Ruby world it\u2019s considered “too little, too late.” It is an incredibly difficult challenge to write a comprehensive test suite at this stage of development. More importantly, the design wins from using a test-first approach have already been missed.<\/p>\n\n\n\n

Test first<\/em> development is when we think about the details of an implementation first, write a test that exercises that implementation, then actually implement it. It follows the “red-green-refactor” cycle, but is kind of cheating since the test was only written to permit the implementation.<\/p>\n\n\n\n

In true test-driven development<\/em>, the test is the important thing. We don\u2019t think of the implementation then write a test, we write a test then figure out a possible implementation. We shift to a goal-oriented focus. When the test is written, the only thing that matters is making it pass. The implementation lives to serve the test.<\/p>\n\n\n\n

Then at the highest level we push towards BDD<\/em>. In testing behavior we focus on business value. TDD focuses on how something will work, BDD focuses on why we build it at all. BDD is a difficult science that we\u2019re still working out how to realize. RSpec, and tools like Capybara and Cucumber are pushing the envelope.<\/p>\n\n\n\n

Testing\/Development Cycle<\/h3>\n\n\n\n

A good cycle to follow for BDD is this outside-in<\/em> approach:<\/p>\n\n\n\n

  1. Write a high-level (outside<\/em>) business value example (using Cucumber or RSpec\/Capybara) that goes red<\/li>
  2. Write a lower-level (inside<\/em>) RSpec example for the first step of implementation that goes red<\/li>
  3. Implement the minimum code to pass that lower-level example, see it go green<\/li>
  4. Write the next lower-level RSpec example pushing towards passing #1<\/li>
  5. Repeat steps #3 and #4 until the high-level test (#1) goes green<\/li><\/ol>\n\n\n\n

    During the process think of your red\/green state as a permission status:<\/p>\n\n\n\n

    When your low-level tests are green<\/em>, you have permission to write new examples or refactor existing implementation. You must not, in the context of that refactoring, add new functionality\/flexibility.<\/p>\n\n\n\n

    When your low-level tests are red<\/em>, you have permission to write or change implmentation code only for the purpose of making the existing tests go green. You must resist the urge to write the code to pass your next<\/em> test, which doesn\u2019t exist, or implement features you\u2019ll need “some day.”<\/p>\n\n\n\n

    High-Level Integration Testing<\/h3>\n\n\n\n

    There are two popular approaches to high-level testing:<\/p>\n\n\n\n