如何在以下代码中为过程方法编写rspec?

时间:2013-03-14 13:27:50

标签: ruby-on-rails-3 rspec

基本上我想确保应该从process方法调用method_1和method_2。

  def process
        begin
          method_1  if some_condition
          method_2  if some_condition      
          self.update_attribute(:status,DONE)
        rescue=>e
          self.update_attribute(:status,ERROR)
          p e
        end
    end

def method_1
#some code
end

def method_2
#some code
end

1 个答案:

答案 0 :(得分:1)

试试这个:

it "should call #method_1" do
  YourClass.should_receive(:method_1)
  YourClass.process
end

it "should call #method_2" do
  YourClass.should_receive(:method_2)
  YourClass.process
end

我假设这些是类方法。

如果这些是实例方法,您可以执行YourClass.any_instance.should_receive(...)your_instance.should_receive(...)

有关详细信息,请参阅http://rubydoc.info/gems/rspec-mocks/frames

修改

should_receive也将存根该方法。这将取消存根并调用方法:

YourClass.should_receive(:method_2).and_call_original