ruby - Testing gems within a Rails App -
i'm attempting test service calling anemone.crawl correctly. have 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) |anemone| end end end
spider_service_spec.rb
require 'spec_helper' require 'anemone' describe spiderservice describe "initialize" let(:url) { mock("url") } subject { spiderservice.new(url) } "should store url in instance variable" subject.url.should == url end end describe "#crawl_site" let(:spider_service) { mock("spider service") } let(:url) { mock("url") } before spiderservice.stub(:new).and_return(spider_service) spider_service.stub(:crawl_site) anemone.stub(:crawl).with(url) end subject { spider_service.crawl_site } "should call anemone.crawl url" anemone.should_receive(:crawl).with(url) subject end end end
and here's error i'm getting, , can't understand, since can call service in rails console , data anemone when provide valid url:
failures: 1) spiderservice#crawl_site should call anemone.crawl 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 silly (i can blame lack of coffee then, instead of general incompetence!)
thank time,
gav
your subject calls method on mock object you're created (mock("spider_service")), not real spiderservice object. you've stubbed call on mock spider service nothing, calling in subject nothing, hence why test fails.
also, you've stubbed new (although never call it) on spiderservice return mock object. when you're testing spiderservice you'll want have real instances of class otherwise method calls not behave on real instance of class.
the following should achieve want:
describe "#crawl_site" let(:spider_service) { spiderservice.new(url) } let(:url) { mock("url") } before anemone.stub(:crawl).with(url) end subject { spider_service.crawl_site } "should call anemone.crawl url" anemone.should_receive(:crawl).with(url) subject end end
you might want move require 'anenome' outside of class definition available elsewhere.
Comments
Post a Comment