Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/22.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 on rails 在Rails应用程序中测试gem_Ruby On Rails_Ruby_Rspec_Gem - Fatal编程技术网

Ruby on rails 在Rails应用程序中测试gem

Ruby on rails 在Rails应用程序中测试gem,ruby-on-rails,ruby,rspec,gem,Ruby On Rails,Ruby,Rspec,Gem,我正在尝试测试我的服务是否正确调用了Anemone.crawl。我有以下代码: 蜘蛛网服务.rb class SpiderService < BaseService require 'anemone' attr_accessor :url def initialize(url) self.url = url end def crawl_site Anemone.crawl(url) do |anemone| end end end requ

我正在尝试测试我的服务是否正确调用了Anemone.crawl。我有以下代码:

蜘蛛网服务.rb

class SpiderService < BaseService
  require 'anemone'
  attr_accessor :url
  def initialize(url)
    self.url = url
  end
  def crawl_site
    Anemone.crawl(url) do |anemone|
    end
  end
end
require 'spec_helper'
require 'anemone'

describe SpiderService do
  describe "initialize" do
    let(:url) { mock("url") }

    subject { SpiderService.new(url) }

    it "should store the url in an instance variable" do
      subject.url.should == url
    end
  end

  describe "#crawl_site" do
    let(:spider_service) { mock("spider service") }
    let(:url) { mock("url") }

    before do
      SpiderService.stub(:new).and_return(spider_service)
      spider_service.stub(:crawl_site)
      Anemone.stub(:crawl).with(url)
    end

    subject { spider_service.crawl_site }

    it "should call Anemone.crawl with the url" do
      Anemone.should_receive(:crawl).with(url)
      subject
    end

  end
end
这是我得到的错误,我无法理解,因为我可以在Rails控制台中调用该服务,当我提供一个有效的URL时,我从Anemone获取数据:

Failures:

  1) SpiderService#crawl_site should call Anemone.crawl with the url
     Failure/Error: Anemone.should_receive(:crawl).with(url)
     (Anemone).crawl(#<RSpec::Mocks::Mock:0x82bdd454 @name="url">)
         expected: 1 time
         received: 0 times
     # ./spec/services/spider_service_spec.rb:28
故障:
1) SpiderService#crawl_站点应使用url调用Anemone.crawl
失败/错误:海葵。应接收(:爬网)。带有(url)
(海葵)爬行
预计:1次
收到:0次
#./spec/services/spider\u service\u spec.rb:28
请告诉我,我忘记了一些愚蠢的事情(我可以怪咖啡不够,而不是一般的无能!)

谢谢你抽出时间


Gav

您的主题在您创建的模拟对象(mock(“spider_服务”)上调用一个方法,而不是真正的SpiderService对象。您还中断了对mock spider服务的调用,使其不执行任何操作,因此在subject中调用它将不会执行任何操作,这就是测试失败的原因

此外,您还在SpiderService上插入了new(尽管您从未调用它)以返回一个模拟对象。当您测试SpiderService时,您需要类的真实实例,否则方法调用的行为将不会像在类的真实实例上那样

以下内容应能实现您的目标:

describe "#crawl_site" do
  let(:spider_service) { SpiderService.new(url) }
  let(:url) { mock("url") }

  before do
    Anemone.stub(:crawl).with(url)
  end

  subject { spider_service.crawl_site }

  it "should call Anemone.crawl with the url" do
    Anemone.should_receive(:crawl).with(url)
    subject
  end

end

您可能还想将require'anenome'移到类定义之外,这样它就可以在其他地方使用。

顺便说一句,这个问题实际上与Rails无关。为了让它工作,我写了很多不同的方法,但在其中的某个地方完全迷失了方向。谢谢你救了我,詹姆斯-完全开悟现在已经不远了!