blob: 2c4e2203fff1ad4194367b3b2b7d9e4fc342689f (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
require 'elasticsearch/model'
require 'hashie'
RSpec.configure do |config|
config.expect_with :rspec do |expect|
expect.syntax = :expect
end
end
class MyModel < Hashie::Mash
include Elasticsearch::Model
disable_warnings
index_name 'model'
document_type 'model'
end
RSpec.describe 'elaasticsearch-model' do
# See https://github.com/intridea/hashie/issues/354#issuecomment-363306114
# for the reason why this doesn't work as you would expect
it 'raises an error when the model does has an id' do
object = MyModel.new
stub_elasticsearch_client
expect { object.__elasticsearch__.index_document }.to raise_error(NoMethodError)
end
it 'does not raise an error when the model has an id' do
object = MyModel.new(id: 123)
stub_elasticsearch_client
expect { object.__elasticsearch__.index_document }.not_to raise_error
end
def stub_elasticsearch_client
response = double('Response', body: '{}')
allow_any_instance_of(Elasticsearch::Transport::Client).to receive(:perform_request) { response }
end
end
|