Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.
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.
|
Code View:
Scroll
/
Show All 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
|