Rspec存根公共方法

时间:2014-02-01 01:29:29

标签: ruby-on-rails rspec

你能不能帮我理解如何在Rails中使用Rspec存根公共方法。

class MyClass
  def start
    result = continue
    result << ' morning glory'
  end

  def continue
    'some text'
  end
end

添加规范

context '#start' do
  let(:myclass) { MyClass.new }
  let(:result)  { "What*s the story morning glory" }

  **1 variant(not working)**
    before { myclass.stub(:continue) { "What*s the story" } }

  **2 variant(not working)**
    before { MyClass.any_instance.stub(:continue) { "What*s the story" } }

  it { expect(myclass.start).to eql result }
end

您有任何想法如何解决这个问题吗?

感谢。

1 个答案:

答案 0 :(得分:1)

before(:each) { MyClass.any_instance.stub(:continue).and_return "What*s the story" }