【问题标题】:Can't stub Curl method无法存根 Curl 方法
【发布时间】:2013-11-03 19:54:15
【问题描述】:

这是我的测试:

# 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>

知道为什么我的存根不起作用吗?

【问题讨论】:

    标签: ruby-on-rails ruby rspec stub


    【解决方案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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-24
      相关资源
      最近更新 更多