describe vs. contextcontext namingbefore vs. letbefore & contextlet vs. subjectbefore vs. letsubject & describedescribe 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. contextdescribe and context group related tests together.
describe vs. contextdescribe for things.context for states.describe vs. contextcontext "#matriculate"
describe "#matriculate"
describe vs. contextdescribe "when the student is sick"
context "when the student is sick"
describe vs. contextcontext isn't available at the top-level.contextcontext 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. letbefore eagerly runs code before each test.let lazily runs code when it is first used.before vs. letbefore for actions.let for dependencies (real or test double).before vs. letlet(:dummy) do
@classroom.initialize_roster
end
before do
@classroom.initialize_roster
end
let is lazy; watch out
for let!.
before vs. letbefore { @grade_levels = [1, 2, 3] }
let(:grade_levels) { [1, 2, 3] }
before aligns with the contextcontext is used for state.before lists the actions to get to that state.before & contextcontext "the bathroom queue is full" do
before do
100.times { bathroom_queue << nil }
end
end
before & contextcontext "the bathroom queue is full" do
before do
bathroom_queue.stub(:full?).and_return(true)
end
end
let vs. subjectlet defines a named variable.subject
describe.shoulds.let vs. subjectsubject for the thing you are testinglet for dependencies.before vs. letlet(: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 describedescribe 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/
#