Module: CrudControllerTestHelper::ClassMethods
- Defined in:
- lib/generators/dry_crud/templates/spec/support/crud_controller_test_helper.rb
Overview
Helper methods to describe contexts.
Instance Method Summary collapse
-
#describe_action(method, action, metadata = {}, &block) ⇒ Object
Describe a certain action and provide some usefull metadata.
-
#it_is_expected_to_have_flash(type, message = nil) ⇒ Object
Test that the given flash type is present.
-
#it_is_expected_to_not_have_flash(type) ⇒ Object
Test that not flash of the given type is present.
-
#it_is_expected_to_persist_entry(persist: true) ⇒ Object
Test that the current entry is persistend and valid, or not.
-
#it_is_expected_to_redirect_to_index ⇒ Object
Test that the response redirects to the index action.
-
#it_is_expected_to_redirect_to_show ⇒ Object
Test that the response redirects to the show action of the current entry.
-
#it_is_expected_to_render_json ⇒ Object
Test that a json response is rendered.
-
#it_is_expected_to_respond(status = 200) ⇒ Object
Test the response status, default 200.
-
#it_is_expected_to_set_attrs(action = nil) ⇒ Object
Test that test_entry_attrs are set on entry.
-
#skip?(options, *contexts) ⇒ Boolean
Is the current context part of the skip list.
Instance Method Details
#describe_action(method, action, metadata = {}, &block) ⇒ Object
Describe a certain action and provide some usefull metadata. Tests whether this action is configured to be skipped.
82 83 84 85 86 87 88 89 90 91 |
# File 'lib/generators/dry_crud/templates/spec/support/crud_controller_test_helper.rb', line 82 def describe_action(method, action, = {}, &block) action_defined = described_class.instance_methods .map(&:to_s) .include?(action.to_s) describe("#{method.to_s.upcase} #{action}", { if: action_defined, method: method, action: action }.merge(), &block) end |
#it_is_expected_to_have_flash(type, message = nil) ⇒ Object
Test that the given flash type is present.
144 145 146 147 148 |
# File 'lib/generators/dry_crud/templates/spec/support/crud_controller_test_helper.rb', line 144 def it_is_expected_to_have_flash(type, = nil) it "flash(#{type}) is set" do expect(flash[type]).to( ? match() : be_present) end end |
#it_is_expected_to_not_have_flash(type) ⇒ Object
Test that not flash of the given type is present.
151 152 153 154 155 |
# File 'lib/generators/dry_crud/templates/spec/support/crud_controller_test_helper.rb', line 151 def it_is_expected_to_not_have_flash(type) it "flash(#{type}) is nil" do expect(flash[type]).to be_blank end end |
#it_is_expected_to_persist_entry(persist: true) ⇒ Object
Test that the current entry is persistend and valid, or not.
158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/generators/dry_crud/templates/spec/support/crud_controller_test_helper.rb', line 158 def it_is_expected_to_persist_entry(persist: true) context "entry" do subject { entry } if persist it { is_expected.not_to be_new_record } it { is_expected.to be_valid } else it { is_expected.to be_new_record } end end end |
#it_is_expected_to_redirect_to_index ⇒ Object
Test that the response redirects to the index action.
127 128 129 130 131 132 133 |
# File 'lib/generators/dry_crud/templates/spec/support/crud_controller_test_helper.rb', line 127 def it_is_expected_to_redirect_to_index it do is_expected.to redirect_to scope_params.merge(action: "index", id: nil, returning: true) end end |
#it_is_expected_to_redirect_to_show ⇒ Object
Test that the response redirects to the show action of the current entry.
136 137 138 139 140 141 |
# File 'lib/generators/dry_crud/templates/spec/support/crud_controller_test_helper.rb', line 136 def it_is_expected_to_redirect_to_show it do is_expected.to redirect_to scope_params.merge(action: "show", id: entry.id) end end |
#it_is_expected_to_render_json ⇒ Object
Test that a json response is rendered.
110 111 112 |
# File 'lib/generators/dry_crud/templates/spec/support/crud_controller_test_helper.rb', line 110 def it_is_expected_to_render_json it { expect(response.body).to start_with("{") } end |
#it_is_expected_to_respond(status = 200) ⇒ Object
Test the response status, default 200.
105 106 107 |
# File 'lib/generators/dry_crud/templates/spec/support/crud_controller_test_helper.rb', line 105 def it_is_expected_to_respond(status = 200) it { expect(response.status).to eq(status) } end |
#it_is_expected_to_set_attrs(action = nil) ⇒ Object
Test that test_entry_attrs are set on entry.
115 116 117 118 119 120 121 122 123 124 |
# File 'lib/generators/dry_crud/templates/spec/support/crud_controller_test_helper.rb', line 115 def it_is_expected_to_set_attrs(action = nil) it "sets params as entry attributes" do attrs = send(:"#{action}_entry_attrs") actual = {} attrs.each_key do |key| actual[key] = entry.attributes[key.to_s] end expect(actual).to eq(attrs) end end |
#skip?(options, *contexts) ⇒ Boolean
Is the current context part of the skip list.
94 95 96 97 98 99 100 101 102 |
# File 'lib/generators/dry_crud/templates/spec/support/crud_controller_test_helper.rb', line 94 def skip?(, *contexts) ||= {} contexts = Array(contexts).flatten skips = Array([:skip]) skips = [ skips ] if skips.blank? || !skips.first.is_a?(Array) skips.flatten.present? && skips.any? { |skip| skip == contexts.take(skip.size) } end |