Module: Hmibo::TestHelpers

Defined in:
lib/hmibo/test_helpers.rb

Overview

Test helpers for RSpec testing of Hmibo services

Instance Method Summary collapse

Instance Method Details

#expect_service_error(service, message) ⇒ Object

Assert that a service has a specific error message



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/hmibo/test_helpers.rb', line 27

def expect_service_error(service, message)
  expect(service.errors?).to be(true),
                             'Expected service to have errors, but it succeeded'

  error_messages = service.errors.map do |error|
    error.is_a?(Hash) ? error[:message] : error.to_s
  end

  expect(error_messages).to include(message),
                            "Expected error '#{message}' but got: #{error_messages}"

  service
end

#expect_service_error_with_attributes(service, attributes = {}) ⇒ Object

Assert that a service has errors with specific attributes



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/hmibo/test_helpers.rb', line 42

def expect_service_error_with_attributes(service, attributes = {})
  expect(service.errors?).to be(true),
                             'Expected service to have errors, but it succeeded'

  matching_error = service.errors.find do |error|
    next false unless error.is_a?(Hash)

    attributes.all? { |key, value| error[key] == value }
  end

  expect(matching_error).to be_present,
                            "Expected error with attributes #{attributes} but got: #{service.errors}"

  service
end

#expect_service_failure(service, expected_error_count: nil) ⇒ Object

Assert that a service failed with errors



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/hmibo/test_helpers.rb', line 14

def expect_service_failure(service, expected_error_count: nil)
  expect(service.errors?).to be(true),
                             "Expected service to fail, but it succeeded with data: #{service.data}"

  if expected_error_count
    expect(service.errors.length).to eq(expected_error_count),
                                     "Expected #{expected_error_count} errors, but got #{service.errors.length}: #{service.errors}"
  end

  service
end

#expect_service_success(service) ⇒ Object

Assert that a service executed successfully



7
8
9
10
11
# File 'lib/hmibo/test_helpers.rb', line 7

def expect_service_success(service)
  expect(service.errors?).to be(false),
                             "Expected service to succeed, but got errors: #{service.errors}"
  service
end

#mock_service(success: true, data: nil, errors: []) ⇒ Object

Create a mock service for testing



59
60
61
62
63
64
65
# File 'lib/hmibo/test_helpers.rb', line 59

def mock_service(success: true, data: nil, errors: [])
  service = instance_double('MockService')
  allow(service).to receive(:errors?).and_return(!errors.empty?)
  allow(service).to receive(:data).and_return(data)
  allow(service).to receive(:errors).and_return(errors)
  service
end

#stub_service(service_class, success: true, data: nil, errors: []) ⇒ Object

Stub a service class to return a specific result



68
69
70
71
72
# File 'lib/hmibo/test_helpers.rb', line 68

def stub_service(service_class, success: true, data: nil, errors: [])
  mock = mock_service(success: success, data: data, errors: errors)
  allow(service_class).to receive(:call).and_return(mock)
  mock
end