describe
vs. context
context
namingbefore
vs. let
before
& context
let
vs. subject
before
vs. let
subject
& describe
describe Classroom do
context "#a_mess?" do
it "should return true after a party" do
classroom = Classroom.new
classroom.throw_pizza_party
classroom.a_mess?.should be_true
end
end
end
describe
vs. context
describe
and context
group related tests together.
describe
vs. context
describe
for things.context
for states.describe
vs. context
context "#matriculate"
describe "#matriculate"
describe
vs. context
describe "when the student is sick"
context "when the student is sick"
describe
vs. context
context
isn't available at the top-level.context
context
blocks better represent this.describe
can be useful for complex return values.context
namingcontext
names should not match your method names!context
namingdescribe "#assign"
context "assigning homework to a student"
before
vs. let
before
eagerly runs code before each test.let
lazily runs code when it is first used.before
vs. let
before
for actions.let
for dependencies (real or test double).before
vs. let
let(:dummy) do
@classroom.initialize_roster
end
before do
@classroom.initialize_roster
end
let
is lazy; watch out
for let!
.
before
vs. let
before { @grade_levels = [1, 2, 3] }
let(:grade_levels) { [1, 2, 3] }
before
aligns with the context
context
is used for state.before
lists the actions to get to that state.before
& context
context "the bathroom queue is full" do
before do
100.times { bathroom_queue << nil }
end
end
before
& context
context "the bathroom queue is full" do
before do
bathroom_queue.stub(:full?).and_return(true)
end
end
let
vs. subject
let
defines a named variable.subject
describe
.should
s.let
vs. subject
subject
for the thing you are testinglet
for dependencies.before
vs. let
let(:classroom) { Classroom.new(grade: 5) }
subject(:classroom) { Classroom.new(grade: 5) }
subject
, but giving it a
name is still a good idea.
subject
aligns with the describe
describe
is used for things.subject
specifies the thing to test.subject
& describe
describe ParentNotifier do
subject(:notifier) do
ParentNotifier.new(phone: '1.555.555.5555')
end
end
subject
& describe
describe ParentNotifier do
subject(:notifier) do
ParentNotifier.new(phone: '1.555.555.5555')
end
describe "the result of calling the parent" do
subject(:call_result) { notifier.call }
end
end
it "should add the student to the class"
it "adds the student to the class"
it "..." do
homework.should be_available
homework.should_not be_expired
end
it "..." { homework.should be_available }
it "..." { homework.should_not be_expired }
it
blocks here.
before do
school_board.should_receive(:funding)
.and_return(100_000)
end
it { should be_funded }
before do
school_board.stub(:funding)
.and_return(100_000)
end
it { should be_funded }
it "..." do
lunch.size.should eql(3)
lunch[1].should be_salted
end
it { should include_fries }
RSpec::Matchers.define :include_fries
match do |actual|
actual.size == 3 && actual[1].salted?
end
end
describe Classroom do
context "#a_mess?" do
it "should return true after a party" do
classroom = Classroom.new
classroom.throw_pizza_party
classroom.a_mess?.should be_true
end
end
end
describe Classroom do
subject(:classroom) { Classroom.new }
context "when the children are rowdy" do
before { classroom.throw_pizza_party }
it { should be_a_mess }
end
end
Classroom
#a_mess?
should return true after a party
Classroom
when the children are rowdy
should be a mess
context
can't be used at the top-level.subject
isn't based on let
.subject
can take a name.feature
and scenario
for feature specs.match_array
/
#