vendredi 25 septembre 2015

How to test service object methods are called?

I'm quite new to RSpec and trying to build some tests for my service objects.

My service file is as follows...

  class FoursquareService

  def initialize(address)
    @coordinates = coordinates(address)
    @category_id = "4bf58dd8d48988d1e0931735"
    @api_version = "20150922"
    @radius = 1000
  end

  def popular_shops
    return unless @coordinates
    response = foursquare_client.explore_venues(search_options)
    venues = response.groups.find { |k,v| k["type"] == "Recommended Places" }.items.map(&:venue)
    venues.sort_by! { |venue| venue.rating.to_f }.reverse!
    venues
  end

  private

  def search_options
    search_options = {
      :ll         => @coordinates,
      :categoryId => @category_id,
      :radius     => @radius
    }
  end

  def coordinates(address)
    Geocoder.coordinates(address).try(:join, ", ")
  end

  def foursquare_client
    Foursquare2::Client.new(
        :client_id      => ENV["FOURSQUARE_ID"],
        :client_secret  => ENV["FOURSQUARE_SECRET"],
        :api_version    => @api_version)
  end

end

I want to test that the private methods are called by the public methods. This is my code...

subject { FoursquareService.new("London") }

it "receives coordinates" do
  expect(subject).to receive(:coordinates)
  subject
end

it "receives search_options" do
  expect(subject).to receive(:search_options)
  subject.popular_shops
end

But I get this error...

expected: 1 time with any arguments
received: 0 times with any arguments

What am I doing wrong here?

Aucun commentaire:

Enregistrer un commentaire