Class: Rails::AddOns::Shoulda::Matchers::ImplementUpdateActionMatcher

Inherits:
Object
  • Object
show all
Includes:
RSpec::Matchers
Defined in:
lib/rails/add_ons/shoulda/matchers/implement_update_action_matcher.rb

Instance Method Summary collapse

Constructor Details

#initialize(spec) ⇒ ImplementUpdateActionMatcher

Returns a new instance of ImplementUpdateActionMatcher.



29
30
31
# File 'lib/rails/add_ons/shoulda/matchers/implement_update_action_matcher.rb', line 29

def initialize(spec)
  @spec = spec
end

Instance Method Details

#descriptionObject



134
135
136
# File 'lib/rails/add_ons/shoulda/matchers/implement_update_action_matcher.rb', line 134

def description
  "expose update action on #{@edit_path}"
end

#failure_messageObject



124
125
126
# File 'lib/rails/add_ons/shoulda/matchers/implement_update_action_matcher.rb', line 124

def failure_message
  "Should expose update action on #{@edit_path}. Error: #{@error}"
end

#failure_message_when_negatedObject Also known as: negative_failure_message



128
129
130
# File 'lib/rails/add_ons/shoulda/matchers/implement_update_action_matcher.rb', line 128

def failure_message_when_negated
  "Should not expose update action on #{@edit_path}. Error: #{@error}"
end

#for(resource) ⇒ Object

Resource that will be updated



34
35
36
37
# File 'lib/rails/add_ons/shoulda/matchers/implement_update_action_matcher.rb', line 34

def for(resource)
  @resource = resource
  self
end

#from(attributes) ⇒ Object



39
40
41
42
# File 'lib/rails/add_ons/shoulda/matchers/implement_update_action_matcher.rb', line 39

def from(attributes)
  @expected_before_attributes = attributes
  self
end

#has_correct_attributes_afterObject



114
115
116
117
118
119
120
121
122
# File 'lib/rails/add_ons/shoulda/matchers/implement_update_action_matcher.rb', line 114

def has_correct_attributes_after
  sliced_resource_attributes = @resource.attributes.with_indifferent_access.slice(*@expected_after_attributes.keys)
  if @expected_after_attributes == sliced_resource_attributes
    true
  else
    @error = "Attributes after update [#{sliced_resource_attributes}] did not match expected attributes [#{@expected_after_attributes}]"
    false
  end
end

#has_correct_attributes_beforeObject



104
105
106
107
108
109
110
111
112
# File 'lib/rails/add_ons/shoulda/matchers/implement_update_action_matcher.rb', line 104

def has_correct_attributes_before
  sliced_resource_attributes = @resource.attributes.with_indifferent_access.slice(*@expected_before_attributes.keys)
  if @expected_before_attributes == sliced_resource_attributes
    true
  else
    @error = "Attributes before update [#{sliced_resource_attributes}] did not match expected attributes [#{@expected_before_attributes}]"
    false
  end
end

#has_correct_current_pathObject



95
96
97
98
99
100
101
102
# File 'lib/rails/add_ons/shoulda/matchers/implement_update_action_matcher.rb', line 95

def has_correct_current_path
  if @spec.current_path == @expected_path
    true
  else
    @error = "Wrong current path [#{@spec.current_path}] instead of [#{@expected_path}]"
    false
  end
end

#has_correct_status_codeObject



86
87
88
89
90
91
92
93
# File 'lib/rails/add_ons/shoulda/matchers/implement_update_action_matcher.rb', line 86

def has_correct_status_code
  if @spec.status_code == 200
    true
  else
    @error = "Wrong status code [#{@spec.status_code}] instead of [200]"
    false
  end
end

#idObject



61
62
63
# File 'lib/rails/add_ons/shoulda/matchers/implement_update_action_matcher.rb', line 61

def id
  @resource.to_param
end

#matches?(base_path) ⇒ Boolean

Returns:

  • (Boolean)


65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/rails/add_ons/shoulda/matchers/implement_update_action_matcher.rb', line 65

def matches?(base_path)
  @base_path = base_path
  @show_path = "#{@base_path}/#{id}"
  @edit_path = "#{@base_path}/#{id}/edit"

  @expected_path = @show_path

  @spec.visit(@edit_path)

  return unless has_correct_attributes_before if @expected_before_attributes.present?

  @spec.within(@form_id) do
    @form_block.call
    @spec.find('input[name="commit"]').click
  end
              
  @resource.reload

  has_correct_status_code && has_correct_current_path && has_correct_attributes_after
end

#to(attributes) ⇒ Object



44
45
46
47
# File 'lib/rails/add_ons/shoulda/matchers/implement_update_action_matcher.rb', line 44

def to(attributes)
  @expected_after_attributes = attributes
  self
end

#updating(&block) ⇒ Object



56
57
58
59
# File 'lib/rails/add_ons/shoulda/matchers/implement_update_action_matcher.rb', line 56

def updating(&block)
  @block = block
  self
end

#within_form(id, &block) ⇒ Object

Specifies the form css id to fill to create the resource.



50
51
52
53
54
# File 'lib/rails/add_ons/shoulda/matchers/implement_update_action_matcher.rb', line 50

def within_form(id, &block)
  @form_id = id
  @form_block = block
  self
end