Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby 关于单元测试rspec的建议_Ruby_Rspec - Fatal编程技术网

Ruby 关于单元测试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') }

您将如何使用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
像这样,您可以单独测试单元及其所有功能

有关详细信息的解释,请参阅和/或