简体   繁体   English

在Rails应用程序中测试gem

[英]Testing gems within a Rails App

I'm attempting to test that my service is calling Anemone.crawl correctly. 我正在尝试测试我的服务是否正确调用了Anemone.crawl。 I have the following code: 我有以下代码:

spider_service.rb spider_service.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

spider_service_spec.rb spider_service_spec.rb

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

And here's the error that I'm getting, and can't understand, since I can call the service in the Rails console and I get back data from Anemone when I provide a valid URL: 这是我正在理解的错误,因为我可以在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

Please tell me I've forgotten something silly (I can blame lack of coffee then, instead of general incompetence!) 请告诉我,我忘记了一些愚蠢的事情(那时我可以责怪缺乏咖啡,而不是一般的无能!)

Thank you for your time, 感谢您的时间,

Gav 加夫

Your subject calls a method on the mock object that you're created (mock("spider_service")), not a real SpiderService object. 您的主题在您创建的模拟对象上调用一个方法(mock(“ spider_service”)),而不是真正的SpiderService对象。 You've also stubbed the call on the mock spider service to do nothing, so calling it in the subject will do nothing, hence why your test fails. 您还使对模拟蜘蛛服务的调用无效,因此在主题中调用它不会执行任何操作,因此为什么测试失败。

Also, you've stubbed new (although you never call it) on SpiderService to return a mock object. 另外,您已在SpiderService上添加了新的(尽管您从未调用过)它以返回模拟对象。 When you're testing SpiderService you'll want to have real instances of the class otherwise method calls will not behave as they would on a real instance of the class. 在测试SpiderService时,您将需要具有该类的真实实例,否则方法调用将不会像在该类的真实实例上那样起作用。

The following should achieve what you want: 以下应该实现您想要的:

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

You might also want to move the require 'anenome' outside of the class definition so it is available elsewhere. 您可能还希望将require'anenome'移到类定义之外,以便在其他位置使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM