您如何使用rspec测试此方法?
def schema
@schema ||= Schema.new(owner, schedules, hour_interval)
end
答案 0 :(得分:1)
如果觉得倾向于问“你试图测试它是什么”,但这里是我的答案:如果你在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
像这样,你可以单独测试该装置及其所有功能。
有关详细信息的说明,请查看The RSpec Documentation和/或Best RSpec Practices