Ruby 关于单元测试rspec的建议
您将如何使用rspec测试此方法Ruby 关于单元测试rspec的建议,ruby,rspec,Ruby,Rspec,您将如何使用rspec测试此方法 def schema @schema ||= Schema.new(owner, schedules, hour_interval) end 如果你想问“你是怎么测试它的”,但我的回答是:如果你是rspec中的单元测试,并且你定义了方法作为你的单元,我建议你这样测试它: describe "schema" do let(:owner) { mock('owner') } let(:schedules) { mock('schedules') }
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
像这样,您可以单独测试单元及其所有功能
有关详细信息的解释,请参阅和/或