Module: CrudControllerTestHelper

Extended by:
ActiveSupport::Concern
Included in:
CrudTestModelsControllerTest
Defined in:
lib/generators/dry_crud/templates/spec/support/crud_controller_test_helper.rb,
lib/generators/dry_crud/templates/test/support/crud_controller_test_helper.rb

Overview

A module to include into your functional tests for your crud controller subclasses. Simply implement the two methods #test_entry and #test_entry_attrs to test the basic crud functionality. Override the test methods if you changed the behaviour in your subclass controller.

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#ivar(name) ⇒ Object


58
59
60
# File 'lib/generators/dry_crud/templates/spec/support/crud_controller_test_helper.rb', line 58

def ivar(name)
  controller.instance_variable_get("@#{name}")
end

#perform_combined_requestObject

If a combine key is given in metadata, only the first request for all examples with the same key will be performed.


23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/generators/dry_crud/templates/spec/support/crud_controller_test_helper.rb', line 23

def perform_combined_request
  stack = RSpec.current_example.[:combine]
  if stack
    @@current_stack ||= nil
    if stack == @@current_stack &&
       described_class == @@current_controller.class
      restore_request
    else
      perform_request
      @@current_stack = stack
      remember_request
    end
  else
    perform_request
  end
end

#perform_requestObject

Performs a request based on the metadata of the action example under test.


7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/generators/dry_crud/templates/spec/support/crud_controller_test_helper.rb', line 7

def perform_request # rubocop:disable Metrics/AbcSize
  m = RSpec.current_example.
  example_params = respond_to?(:params) ? send(:params) : {}
  params = scope_params.dup
  params[:format] = m[:format] if m[:format]
  params[:id] = test_entry.id if m[:id]
  params.merge!(example_params)
  if m[:method] == :get && m[:format] == :js
    get m[:action], params: params, xhr: true
  else
    send(m[:method], m[:action], params: params)
  end
end

#remember_requestObject


40
41
42
43
44
45
46
47
48
49
# File 'lib/generators/dry_crud/templates/spec/support/crud_controller_test_helper.rb', line 40

def remember_request
  @@current_response = @response
  @@current_request = @request
  @@current_controller = @controller
  @@current_templates = @_templates || @templates

  # treat in-memory entry as committed in order to
  # avoid rollback of internal state.
  entry&.committed!
end

#restore_requestObject


51
52
53
54
55
56
# File 'lib/generators/dry_crud/templates/spec/support/crud_controller_test_helper.rb', line 51

def restore_request
  @response = @@current_response
  @_templates = @templates = @@current_templates
  @controller = @@current_controller
  @request = @@current_request
end

#scope_paramsObject

The params defining the nesting of the test entry.


63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/generators/dry_crud/templates/spec/support/crud_controller_test_helper.rb', line 63

def scope_params
  params = {}
  # for nested controllers, add parent ids to each request
  Array(controller.nesting).reverse.reduce(test_entry) do |parent, p|
    if p.is_a?(Class) && p < ActiveRecord::Base
      assoc = p.name.underscore
      params["#{assoc}_id"] = parent.send(:"#{assoc}_id")
      parent.send(assoc)
    else
      parent
    end
  end
  params
end

#test_createObject

:nodoc:


74
75
76
77
78
79
80
81
# File 'lib/generators/dry_crud/templates/test/support/crud_controller_test_helper.rb', line 74

def test_create # :nodoc:
  assert_difference("#{model_class.name}.count") do
    post :create, params: test_params(model_identifier => new_entry_attrs)
  end
  assert_redirected_to_show entry
  assert_not entry.new_record?
  assert_attrs_equal(new_entry_attrs)
end

#test_create_jsonObject

:nodoc:


83
84
85
86
87
88
89
90
# File 'lib/generators/dry_crud/templates/test/support/crud_controller_test_helper.rb', line 83

def test_create_json # :nodoc:
  assert_difference("#{model_class.name}.count") do
    post :create, params: test_params(model_identifier => new_entry_attrs,
                                      format: 'json')
  end
  assert_response :success
  assert @response.body.starts_with?('{"id":')
end

#test_destroyObject

:nodoc:


117
118
119
120
121
122
# File 'lib/generators/dry_crud/templates/test/support/crud_controller_test_helper.rb', line 117

def test_destroy # :nodoc:
  assert_difference("#{model_class.name}.count", -1) do
    delete :destroy, params: test_params(id: test_entry.id)
  end
  assert_redirected_to_index
end

#test_destroy_jsonObject

:nodoc:


124
125
126
127
128
129
130
131
# File 'lib/generators/dry_crud/templates/test/support/crud_controller_test_helper.rb', line 124

def test_destroy_json # :nodoc:
  assert_difference("#{model_class.name}.count", -1) do
    delete :destroy, params: test_params(id: test_entry.id,
                                         format: 'json')
  end
  assert_response :success
  assert_equal '', @response.body.strip
end

#test_editObject

:nodoc:


92
93
94
95
96
# File 'lib/generators/dry_crud/templates/test/support/crud_controller_test_helper.rb', line 92

def test_edit # :nodoc:
  get :edit, params: test_params(id: test_entry.id)
  assert_response :success
  assert_equal test_entry, entry
end

#test_indexObject

:nodoc:


7
8
9
10
11
# File 'lib/generators/dry_crud/templates/test/support/crud_controller_test_helper.rb', line 7

def test_index # :nodoc:
  get :index, params: test_params
  assert_response :success
  assert entries.present?
end

#test_index_jsonObject

:nodoc:


13
14
15
16
17
18
# File 'lib/generators/dry_crud/templates/test/support/crud_controller_test_helper.rb', line 13

def test_index_json # :nodoc:
  get :index, params: test_params(format: 'json')
  assert_response :success
  assert entries.present?
  assert @response.body.starts_with?('[{'), @response.body
end

#test_index_searchObject

rubocop:disable Metrics/AbcSize :nodoc:


20
21
22
23
24
25
26
27
28
29
# File 'lib/generators/dry_crud/templates/test/support/crud_controller_test_helper.rb', line 20

def test_index_search # rubocop:disable Metrics/AbcSize :nodoc:
  field = @controller.search_columns.first
  val = field && test_entry[field].to_s
  return if val.blank? # does not support search or no value in this field

  get :index, params: test_params(q: val[0..((val.size + 1) / 2)])
  assert_response :success
  assert entries.present?
  assert entries.include?(test_entry)
end

#test_index_sort_ascObject

:nodoc:


31
32
33
34
35
36
37
38
# File 'lib/generators/dry_crud/templates/test/support/crud_controller_test_helper.rb', line 31

def test_index_sort_asc # :nodoc:
  col = model_class.column_names.first
  get :index, params: test_params(sort: col, sort_dir: 'asc')
  assert_response :success
  assert entries.present?
  sorted = entries.sort_by(&col.to_sym)
  assert_equal sorted, entries.to_a
end

#test_index_sort_descObject

:nodoc:


40
41
42
43
44
45
46
47
# File 'lib/generators/dry_crud/templates/test/support/crud_controller_test_helper.rb', line 40

def test_index_sort_desc # :nodoc:
  col = model_class.column_names.first
  get :index, params: test_params(sort: col, sort_dir: 'desc')
  assert_response :success
  assert entries.present?
  sorted = entries.to_a.sort_by(&col.to_sym)
  assert_equal sorted.reverse, entries.to_a
end

#test_newObject

:nodoc:


68
69
70
71
72
# File 'lib/generators/dry_crud/templates/test/support/crud_controller_test_helper.rb', line 68

def test_new # :nodoc:
  get :new, params: test_params
  assert_response :success
  assert entry.new_record?
end

#test_showObject

:nodoc:


49
50
51
52
53
# File 'lib/generators/dry_crud/templates/test/support/crud_controller_test_helper.rb', line 49

def test_show # :nodoc:
  get :show, params: test_params(id: test_entry.id)
  assert_response :success
  assert_equal test_entry, entry
end

#test_show_jsonObject

:nodoc:


55
56
57
58
59
60
# File 'lib/generators/dry_crud/templates/test/support/crud_controller_test_helper.rb', line 55

def test_show_json # :nodoc:
  get :show, params: test_params(id: test_entry.id, format: 'json')
  assert_response :success
  assert_equal test_entry, entry
  assert @response.body.starts_with?('{')
end

#test_show_with_non_existing_id_raises_record_not_foundObject

:nodoc:


62
63
64
65
66
# File 'lib/generators/dry_crud/templates/test/support/crud_controller_test_helper.rb', line 62

def test_show_with_non_existing_id_raises_record_not_found # :nodoc:
  assert_raise(ActiveRecord::RecordNotFound) do
    get :show, params: test_params(id: 9999)
  end
end

#test_updateObject

:nodoc:


98
99
100
101
102
103
104
105
# File 'lib/generators/dry_crud/templates/test/support/crud_controller_test_helper.rb', line 98

def test_update # :nodoc:
  assert_no_difference("#{model_class.name}.count") do
    put :update, params: test_params(id: test_entry.id,
                                     model_identifier => edit_entry_attrs)
  end
  assert_attrs_equal(edit_entry_attrs)
  assert_redirected_to_show entry
end

#test_update_jsonObject

:nodoc:


107
108
109
110
111
112
113
114
115
# File 'lib/generators/dry_crud/templates/test/support/crud_controller_test_helper.rb', line 107

def test_update_json # :nodoc:
  assert_no_difference("#{model_class.name}.count") do
    put :update, params: test_params(id: test_entry.id,
                                     model_identifier => edit_entry_attrs,
                                     format: 'json')
  end
  assert_response :success
  assert @response.body.starts_with?('{"id":')
end