Class: Rails::AddOns::Shoulda::Matchers::ImplementCreateActionMatcher

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

Instance Method Summary collapse

Constructor Details

#initialize(spec) ⇒ ImplementCreateActionMatcher


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

def initialize(spec)
  @spec = spec
end

Instance Method Details

#by(expected_increase) ⇒ Object


66
67
68
69
# File 'lib/rails/add_ons/shoulda/matchers/implement_create_action_matcher.rb', line 66

def by(expected_increase)
  @expected_increase = expected_increase
  self
end

#created_resourceObject


106
107
108
# File 'lib/rails/add_ons/shoulda/matchers/implement_create_action_matcher.rb', line 106

def created_resource
  @created_resource ||= @created_resource_finder_block.present? ? @created_resource_finder_block.call : @resource_class.last
end

#descriptionObject


151
152
153
# File 'lib/rails/add_ons/shoulda/matchers/implement_create_action_matcher.rb', line 151

def description
  "expose create action on #{@new_path}"
end

#expected_pathObject


110
111
112
# File 'lib/rails/add_ons/shoulda/matchers/implement_create_action_matcher.rb', line 110

def expected_path
  @expected_path ||= "#{@base_path}/#{created_resource.to_param}"
end

#failure_messageObject


141
142
143
# File 'lib/rails/add_ons/shoulda/matchers/implement_create_action_matcher.rb', line 141

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

#failure_message_when_negatedObject Also known as: negative_failure_message


145
146
147
# File 'lib/rails/add_ons/shoulda/matchers/implement_create_action_matcher.rb', line 145

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

#finding_created_resource_with(&block) ⇒ Object


84
85
86
87
# File 'lib/rails/add_ons/shoulda/matchers/implement_create_action_matcher.rb', line 84

def finding_created_resource_with(&block)
  @created_resource_finder_block = block
  self
end

#for(resource_class) ⇒ Object

Resource class that will be created


72
73
74
75
# File 'lib/rails/add_ons/shoulda/matchers/implement_create_action_matcher.rb', line 72

def for(resource_class)
  @resource_class = resource_class
  self
end

#has_correct_current_pathObject


132
133
134
135
136
137
138
139
# File 'lib/rails/add_ons/shoulda/matchers/implement_create_action_matcher.rb', line 132

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


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

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

#has_increased_resource_countObject


123
124
125
126
127
128
129
130
# File 'lib/rails/add_ons/shoulda/matchers/implement_create_action_matcher.rb', line 123

def has_increased_resource_count
  if (@after_count - @before_count) == @expected_increase
    true
  else
    @error = "Did not increase by expected [#{@expected_increase}] but by [#{@after_count - @before_count}]"
    false
  end
end

#id(id) ⇒ Object


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

def id(id)
  @id = id
  self
end

#increasing(&block) ⇒ Object


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

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

#matches?(base_path) ⇒ Boolean


89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/rails/add_ons/shoulda/matchers/implement_create_action_matcher.rb', line 89

def matches?(base_path)
  @base_path = @spec.class.name.split('::')[0..2].join('::').constantize.description
  # @base_path     = base_path
  @new_path = "#{@base_path}/new"

  @spec.visit(@new_path)

  @before_count = @block.call(@resource_class)
  @spec.within(@form_id) do
    @form_block.call
    @spec.find('input[name="commit"]').click
  end
  @after_count = @block.call(@resource_class)

  has_correct_status_code && has_increased_resource_count && has_correct_current_path
end

#within_form(id, &block) ⇒ Object

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


78
79
80
81
82
# File 'lib/rails/add_ons/shoulda/matchers/implement_create_action_matcher.rb', line 78

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