diff --git a/Gemfile b/Gemfile index 80469e798..ceb3ad377 100644 --- a/Gemfile +++ b/Gemfile @@ -15,6 +15,8 @@ gem 'ruby-progressbar', require: false gem 'guard' gem 'guard-rspec' +gem 'pry' + gem 'redcarpet' gem 'yard' diff --git a/lib/chewy/rspec/mock_response_of.rb b/lib/chewy/rspec/mock_response_of.rb new file mode 100644 index 000000000..21b403f75 --- /dev/null +++ b/lib/chewy/rspec/mock_response_of.rb @@ -0,0 +1,25 @@ +RSpec::Matchers.define :mock_response_of do |method, options = {}| + match do |_block| + if defined?(Mocha) && RSpec.configuration.mock_framework.to_s == 'RSpec::Core::MockingAdapters::Mocha' + Chewy::Search::Request.stubs(:new).with([]).returns(mock_request) + else + request_double = double(Chewy::Search::Request) + allow(request_double).to receive(:new).and_return(mock_request) + new_request = request_double.new + allow(new_request).to receive(method).with(options, any_args) + #new_request.__send__(method, options) + end + end + + def mock_request + @mock_request ||= MockRequest.new + end + + class MockRequest + attr_reader :indexes + + def initialize + @indexes = [] + end + end +end diff --git a/spec/chewy/index/actions_spec.rb b/spec/chewy/index/actions_spec.rb index 2092fc85a..c62345b2d 100644 --- a/spec/chewy/index/actions_spec.rb +++ b/spec/chewy/index/actions_spec.rb @@ -803,6 +803,12 @@ let(:dest_index_with_prefix) { 'dummies_dest_index' } let(:unexisting_index) { 'wrong_index' } + context 'mock_test' do + specify do + expect({}).to mock_response_of(:perform) + end + end + context 'with existing indexes' do specify do expect(CitiesIndex) diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 3f2501564..970e488c4 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -16,6 +16,7 @@ require 'support/class_helpers' require 'chewy/rspec' +require 'chewy/rspec/mock_response_of' host = ENV['ES_HOST'] || 'localhost' port = ENV['ES_PORT'] || 9250