不能存根Curl方法

时间:2013-11-03 19:54:15

标签: ruby-on-rails ruby rspec stub

这是我的测试:

# Custom validation method tests
  describe "#ticker_symbol" do
    before(:each) do
      o = OpenStruct.new(:body => '')
      Curl::Easy.any_instance.stub(:get).and_return(o)
    end
    it "should add an error" do
      subject
    end
  end

我的模型的相关部分:

# Custom validation methods
  def ticker_symbol
    apiresponse = Curl.get("https://www.google.com/finance/info?infotype=infoquoteall&q=" + ticker)
    debugger
    if apiresponse.body == ''
      errors.add(:ticker, "must be valid")
    end
  end

出于某种原因,apiresponse不应该是它应该是:

apiresponse
#<Curl::Easy https://www.google.com/finance/info?infotype=infoq>

知道为什么我的存根无法正常工作?

1 个答案:

答案 0 :(得分:1)

# Custom validation method tests
describe "#ticker_symbol" do
  let(:stubbed_response) { OpenStruct.new(:body => '') }
  before(:each) do
    Curl.stub(:get).and_return stubbed_response
  end
  it "should add an error" do
    subject
  end
end