このメソッドをrspecでどのようにテストしますか?
def schema
@schema ||= Schema.new(owner, schedules, hour_interval)
end
「そして、何をテストしようとしたのか」と尋ねたくなった場合でも、とにかく私の答えは次のとおりです。rspecでユニットテストを行っていて、メソッドをユニットとして定義している場合は、次のようにテストすることをお勧めします。
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
このように、ユニットとそれが行うすべてを分離してテストします。
詳細については、RSpec ドキュメント および/またはRSpec のベスト プラクティスを参照してください。