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