0

您将如何使用 rspec 测试此方法?

def schema
  @schema ||= Schema.new(owner, schedules, hour_interval)
end
4

1 回答 1

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

像这样,您单独测试该单元及其所有功能。

有关详细信息的说明,请查看RSpec 文档 和/或最佳 RSpec 实践

于 2013-01-17T14:54:07.900 回答