Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • DownloadDownload
  • PrintPrint
Share this Page URL
Help

Chapter 18. RSpec > Introduction

18.1. Introduction

Since RSpec scripts are so readable, I can’t really think of a better way of introducing you to the framework than to dive into an actual spec. Listing 18.1 is part of a real-world RSpec script defining the behavior of a Payment in a Hashrocket client project named Workbeast.com. As you’re reading the spec, let the descriptions attached to the blocks of code come together to form sentences that describe the desired behavior.

Listing 18.1. Excerpt of Workbeast.com’s timesheet spec

require 'spec_helper'

describe Timesheet do
  let(:timesheet) { Factory(:timesheet) }
describe "validation of hours worked" do
  it "fails without a number" do
    subject.hours_worked = 'abc'
    subject.should have(1).error_on(:hours_worked)
  end

  it "passes with a number" do
    subject.hours_worked = '123'
    subject.should have(0).errors_on(:hours_worked)
  end

end

context "when submitted" do
  it "sends an email notification to the manager" do
    Notifier.should_receive(:send_later).with(
      :deliver_timesheet_submitted, timesheet
    )
    timesheet.submit
  end

  it "notifies its opening" do
    timesheet.opening.should_not be_nil
    timesheet.opening.should_receive(:fill)
    timesheet.submit
  end

end

					  


  

You are currently reading a PREVIEW of this book.

                                                                                        

Get instant access to over
$1 million worth of books and videos.

  

Start a Free Trial


  
  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • DownloadDownload
  • PrintPrint