Gav
Gav

Reputation: 11460

Testing gems within a Rails App

I'm attempting to test that my service is calling Anemone.crawl correctly. I have the following code:

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

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:

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

Upvotes: 0

Views: 236

Answers (1)

JamesAlmond
JamesAlmond

Reputation: 181

Your subject calls a method on the mock object that you're created (mock("spider_service")), not a real SpiderService object. 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. 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.

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.

Upvotes: 1

Related Questions