O'Reilly logo

Effective Testing with RSpec 3 by Myron Marston, Ian Dees

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Filling In the First Spec

You don’t have any classes or methods yet for indicating whether recording an expense succeeded or failed. Let’s take a moment to sketch out what that code would look like.

Connecting to Storage

First, you’ll need some kind of storage engine that keeps the expense history; call it a Ledger. The simplest approach would be for the API class to create a Ledger instance directly:

 class​ API < Sinatra::Base
 def​ initialize
  @ledger = Ledger.new
 super​() ​# rest of initialization from Sinatra
 end
 end
 
 # Later, callers do this:
 app = API.new

But this style limits the code’s flexibility and testability, as it doesn’t allow you to use a substitute ledger for ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required