0
你如何用rspec测试这个方法?关于单元测试的建议rspec
def schema
@schema ||= Schema.new(owner, schedules, hour_interval)
end
你如何用rspec测试这个方法?关于单元测试的建议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
这样,你测试的单位和所有它在隔离。
有关详细说明,请参阅The RSpec Documentation和/或Best RSpec Practices