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

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.


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

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.


145
146
147
148
149
# File 'lib/generators/dry_crud/templates/spec/support/crud_controller_test_helper.rb', line 145

def it_is_expected_to_have_flash(type, message = nil)
  it "flash(#{type}) is set" do
    expect(flash[type]).to(message ? match(message) : be_present)
  end
end

#it_is_expected_to_not_have_flash(type) ⇒ Object

Test that not flash of the given type is present.


152
153
154
155
156
# File 'lib/generators/dry_crud/templates/spec/support/crud_controller_test_helper.rb', line 152

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.


159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/generators/dry_crud/templates/spec/support/crud_controller_test_helper.rb', line 159

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_indexObject

Test that the response redirects to the index action.


128
129
130
131
132
133
134
# File 'lib/generators/dry_crud/templates/spec/support/crud_controller_test_helper.rb', line 128

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_showObject

Test that the response redirects to the show action of the current entry.


137
138
139
140
141
142
# File 'lib/generators/dry_crud/templates/spec/support/crud_controller_test_helper.rb', line 137

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_jsonObject

Test that a json response is rendered.


111
112
113
# File 'lib/generators/dry_crud/templates/spec/support/crud_controller_test_helper.rb', line 111

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.


106
107
108
# File 'lib/generators/dry_crud/templates/spec/support/crud_controller_test_helper.rb', line 106

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.


116
117
118
119
120
121
122
123
124
125
# File 'lib/generators/dry_crud/templates/spec/support/crud_controller_test_helper.rb', line 116

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.

Returns:

  • (Boolean)

95
96
97
98
99
100
101
102
103
# File 'lib/generators/dry_crud/templates/spec/support/crud_controller_test_helper.rb', line 95

def skip?(options, *contexts)
  options ||= {}
  contexts = Array(contexts).flatten
  skips = Array(options[:skip])
  skips = [skips] if skips.blank? || !skips.first.is_a?(Array)

  skips.flatten.present? &&
    skips.any? { |skip| skip == contexts.take(skip.size) }
end