Rspec: Ошибка шпиона при обнаружении вызова метода (мой шаблон неверен) - PullRequest
0 голосов
/ 25 сентября 2019

Как проверить, что метод вызывается из другого метода?

Мне нужно проверить, что ListingQuery.call () вызывает метод base_filter

Код

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)

Ошибка

 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

ОБНОВЛЕНО ТАКЖЕ СБОЙ:

  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)

Ошибка:

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.

Ответы [ 2 ]

0 голосов
/ 25 сентября 2019

Это работает:

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

По какой-то причине allow_any_instance_of не сработало, мне пришлось заглушить конкретный экземпляр.Мне было бы интересно узнать почему, если кто-нибудь знает.

0 голосов
/ 25 сентября 2019

Я думаю, что вы присваиваете переменную so результату ListingsQuery::Search.call(current_user, {}, query_object).Это может работать:

so = ListingsQuery::Search
so.call(current_user, {}, query_object)
expect(so).to have_received(:base_filters)

или как я обычно пишу их:

so = ListingsQuery::Search
expect(so).to receive(:base_filters)
so.call(current_user, {}, query_object)
...