Search code examples
ruby-on-railsrubyrspecrspec-rails

Rspec: Failure of spy to detect method call (my pattern is wrong)


How do I test that a method is being called within another method?

I need to test that ListingQuery.call() calls the base_filter method

Code

module ListingsQuery
  class Search
    def self.call(current_user, params, query_object_override=nil)
      new(current_user, params, query_object_override).call
    end

    def initialize(current_user, params, query_object_override=nil)
      @params = params
      @type = params[:market] || params[:type] || "all"
      @current_user = current_user
      @type = if @type.present?
                @type.downcase.singularize
              else
                "listing"
              end
      @query_object = query_object_override
    end

    def call
      relation_for(query_object)
    end

    private

    def relation_for(query_object_instance)
      if query_object_instance.class == ListingsQuery::Car
        car_filters(query_object_instance).relation
      else
        raise ArgumentError, "ListingsQuery 'Class: #{query_object_instance.class}' does not exist"
      end
    end

    def car_filters(query_object)
      base_filters(query_object)
      other_filters(query_object)
      query_object
    end

    def query_object
      @query_object ||= query_object_for(@type, @current_user) # => ListingQuery::Car
    end
  end
end

### Test

  current_user = spy("User")
  query_object_class = ListingsQuery::Car
  query_object = spy(query_object_class.name)

  allow_any_instance_of(RSpec::Mocks::Double).to receive(:class) { query_object_class }
  allow_any_instance_of(ListingsQuery::Search).to receive(:base_filters) { query_object }
  so = ListingsQuery::Search.call(current_user, {}, query_object)
  expect(so).to have_received(:base_filters)

Error

 1) ListingsQuery::Search#car_filters should call base_filters
     Failure/Error: expect(so).to have_received(:base_filters)

       (Double "ListingsQuery::Car").base_filters(*(any args))
           expected: 1 time with any arguments
           received: 0 times with any arguments

UPDATED ALSO FAILS:

  current_user = spy("User")

  query_object_class  = ListingsQuery::Car
  search_object_class = ListingsQuery::Search

  query_object = spy(query_object_class.name)
  allow_any_instance_of(RSpec::Mocks::Double).to receive(:class) { query_object_class }

  allow_any_instance_of(search_object_class).to receive(:base_filters) { query_object }
  expect(search_object_class).to have_received(:base_filters)
  search_object_class.call(current_user, {}, query_object)

Error:

Failure/Error: expect(search_object_class).to have_received(:base_filters)
       #<ListingsQuery::Search (class)> expected to have received base_filters, but that object is not a spy or method has not been stubbed.

Solution

  • This works:

    it "should call base_filters" do      
      current_user = spy("User")
      query_object_class  = ListingsQuery::Car
      search_object_class = ListingsQuery::Search
      query_object = spy(query_object_class.name)
      allow_any_instance_of(RSpec::Mocks::Double).to receive(:class) { query_object_class }
      so = search_object_class.new(current_user, {}, query_object)
      allow(so).to receive(:base_filters) { query_object }      
      so.call
      expect(so).to have_received(:base_filters)
    end
    

    For some reason allow_any_instance_of did not work, I had to stub the specific instance. I would be curious to know why, if anyone knows.