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 ...

Get Effective Testing with RSpec 3 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.