Reputation: 2105
How would you test this method with rspec?
def schema
@schema ||= Schema.new(owner, schedules, hour_interval)
end
Upvotes: 0
Views: 81
Reputation: 9622
If felt inclined to ask "and what did you try to test it", but here's my answer anyway: If you're unit testing in rspec, and you define methods to be your units, I would suggest to test it like this:
describe "schema" do
let(:owner) { mock('owner') }
let(:schedules) { mock('schedules') }
let(:hour_interval) { mock('hour_interval') }
let(:schema) { mock('schema') }
before(:each) do
subject.stub! :owner => owner, :schedules => schedules, :hour_interval => hour_interval
end
context "unmemoized" do
it "should instantiate a new schema" do
Schema.should_receive(:new).with(owner, schedules, hour_interval).and_return schema
subject.schema.should == schema
end
end
context "memoized" do
it "should use the instantiated and memoized schema" do
Schema.should_receive(:new).with(owner, schedules, hour_interval).once.and_return schema
2.times do
subject.schema.should == schema
end
end
end
end
Like this, you test the unit and all it does in isolation.
For explanation regarding the details, look at The RSpec Documentation and/or Best RSpec Practices
Upvotes: 1