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

Returns a new instance of ImplementCreateActionMatcher.



25
26
27
# File 'lib/rails/add_ons/shoulda/matchers/implement_create_action_matcher.rb', line 25

def initialize(spec)
  @spec = spec
end

Instance Method Details

#by(expected_increase) ⇒ Object



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

def by(expected_increase)
  @expected_increase = expected_increase
  self
end

#created_resourceObject



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

def created_resource
  @created_resource ||= @resource_class.first
end

#descriptionObject



118
119
120
# File 'lib/rails/add_ons/shoulda/matchers/implement_create_action_matcher.rb', line 118

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

#expected_pathObject



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

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

#failure_messageObject



108
109
110
# File 'lib/rails/add_ons/shoulda/matchers/implement_create_action_matcher.rb', line 108

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

#failure_message_when_negatedObject Also known as: negative_failure_message



112
113
114
# File 'lib/rails/add_ons/shoulda/matchers/implement_create_action_matcher.rb', line 112

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

#for(resource_class) ⇒ Object

Resource class that will be created



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

def for(resource_class)
  @resource_class = resource_class
  self
end

#has_correct_current_pathObject



99
100
101
102
103
104
105
106
# File 'lib/rails/add_ons/shoulda/matchers/implement_create_action_matcher.rb', line 99

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



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

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



90
91
92
93
94
95
96
97
# File 'lib/rails/add_ons/shoulda/matchers/implement_create_action_matcher.rb', line 90

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

#id(id) ⇒ Object



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

def id(id)
  @id = id
  self
end

#increasing(&block) ⇒ Object



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

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

#matches?(base_path) ⇒ Boolean

Returns:

  • (Boolean)


57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/rails/add_ons/shoulda/matchers/implement_create_action_matcher.rb', line 57

def matches?(base_path)
  @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_correct_current_path && has_increased_resource_count
end

#within_form(id, &block) ⇒ Object

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



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

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