Saving Your Progress: Pending Specs

Before veering off into low-level implementation, it’s a good idea to save your work so far. We don’t recommend leaving specs in a failing state, though. So let’s mark this one as in progress. Add the following highlighted line at the top of your spec:

 it​ ​'records submitted expenses'​ ​do
» pending ​'Need to persist expenses'

Now, when you run your specs, you will get a reminder of what you were working on:

 $ ​​bundle exec rspec
 *
 
 Pending: (Failures listed here are expected and do not affect your suite's
 status)
 
  1) Expense Tracker API records submitted expenses
  # Need to persist expenses
  Failure/Error: expect(expenses).to contain_exactly(coffee, zoo)
 
  expected collection contained: [{"payee"=>"Starbucks",
  "amount"=>5.75, "date"=>"2017-06-10", "id"=>42}, {"payee"=>"Zoo",
  "amount"=>15.25, "date"=>"2017-06-10", "id"=>42}]
  actual collection contained: []
  the missing elements were: [{"payee"=>"Starbucks",
  "amount"=>5.75, "date"=>"2017-06-10", "id"=>42}, {"payee"=>"Zoo",
  "amount"=>15.25, "date"=>"2017-06-10", "id"=>42}]
  # ./spec/acceptance/expense_tracker_api_spec.rb:46:in ‘block (2
  levels) in <module:ExpenseTracker>’
 
 Finished in 0.03437 seconds (files took 0.1271 seconds to load)
 1 example, 0 failures, 1 pending

Once you implement that behavior, you’ll take out the pending line. RSpec will fail your specs if you forget—in effect, it’s saying, “Hey, you said this wasn’t working yet!”

Before moving on, let’s hook our application up to a web server so we can actually see it working. Rack, the HTTP toolkit that Sinatra is built on top of, ships with a tool named rackup that makes it easy to run any Rack application (including apps built using Sinatra). We just need to define a rackup config file named config.ru with the following contents:

 require_relative ​'app/api'
 run ExpenseTracker::API.new

Simple enough. We’re just loading our application and telling Rack to run it. With that in place, we can boot our application by running rackup:

 $ ​​bundle​​ ​​exec​​ ​​rackup
 [2017-06-13 13:34:10] INFO WEBrick 1.3.1
 [2017-06-13 13:34:10] INFO ruby 2.4.1 (2017-03-22) [x86_64-darwin15]
 [2017-06-13 13:34:10] INFO WEBrick::HTTPServer​#start: pid=45203 port=9292

While that’s running, we can use a command-line tool like curl in another terminal window to send requests to our application.[32] The port=9292 bit from that last line tells us what port our application is running on, so let’s send a request to localhost:9292:

 $ ​​curl localhost:9292/expenses/2017-06-10 -w " "
 []

It works! As expected, our application is responding with an empty JSON array.

Before moving on, commit the code into your favorite revision control system. That way, you’ll easily be able to pick up where you left off. Grab a cup of coffee, try a couple of the exercises, and meet us in the next chapter.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset