Class: Peck::Should

Inherits:
Object
  • Object
show all
Defined in:
lib/peck_on_rails/assertions.rb

Defined Under Namespace

Classes: Disallow, RequireLogin, Response, ResponseRequirement, Specification

Instance Method Summary collapse

Instance Method Details

#equal_keys(other) ⇒ Object



162
163
164
165
166
167
168
169
170
# File 'lib/peck_on_rails/assertions.rb', line 162

def equal_keys(other)
  left = @this.keys.map(&:to_s).sort
  right = other.map(&:to_s).sort

  missing_from_left = left - right
  missing_from_right = right - left
  message = "Expected the object to #{!@negated ? 'have' : 'not have'} the same keys:\n#{left.inspect}\n#{right.inspect}\n>>> #{missing_from_left.inspect}\n<<< #{missing_from_right.inspect}"
  satisfy(message) { left == right }
end

#equal_record_array(*others) ⇒ Object



144
145
146
147
148
149
150
151
# File 'lib/peck_on_rails/assertions.rb', line 144

def equal_record_array(*others)
  left, right = @this, others
  left.flatten! if left.respond_to?(:flatten)
  right.flatten! if right.respond_to?(:flatten)

  message = "Expected the array of records to be #{!@negated ? 'equal' : 'unequal'}: #{left.map(&:id).inspect} - #{right.map(&:id).inspect}"
  satisfy(message) { left == right }
end

#equal_record_set(*others) ⇒ Object



135
136
137
138
139
140
141
142
# File 'lib/peck_on_rails/assertions.rb', line 135

def equal_record_set(*others)
  left, right = @this, others
  left.flatten! if left.respond_to?(:flatten)
  right.flatten! if right.respond_to?(:flatten)

  message = "Expected the record set to be #{!@negated ? 'equal' : 'unequal'}: #{left.map(&:id).inspect} - #{right.map(&:id).inspect}"
  satisfy(message) { Set.new(left) == Set.new(right) }
end

#equal_set(*others) ⇒ Object



153
154
155
156
157
158
159
160
# File 'lib/peck_on_rails/assertions.rb', line 153

def equal_set(*others)
  left, right = @this, others
  left.flatten! if left.respond_to?(:flatten)
  right.flatten! if right.respond_to?(:flatten)

  message = "Expected sets to be #{!@negated ? 'equal' : 'unequal'}: #{left.inspect} - #{right.inspect}"
  satisfy(message) { Set.new(left) == Set.new(right) }
end

#redirect_to(somewhere) ⇒ Object



172
173
174
175
176
# File 'lib/peck_on_rails/assertions.rb', line 172

def redirect_to(somewhere)
  message = "Expected to redirect to `#{somewhere}'"
  response = @this.send(:response)
  satisfy(message) { [301, 302].include?(response.status.to_i) && response.location == somewhere }
end

#validate_with(attribute, value) ⇒ Object



178
179
180
181
182
183
184
185
186
187
188
# File 'lib/peck_on_rails/assertions.rb', line 178

def validate_with(attribute, value)
  message = "Expected #{!@negated ? 'no' : ''}errors on #{attribute.inspect} with value `#{value.inspect}' after validation"

  @this.send("#{attribute}=", value)
  @this.valid?
  if @this.errors[attribute].kind_of?(Array)
    satisfy(message) { @this.errors[attribute].empty? }
  else
    satisfy(message) { @this.errors[attribute].nil? }
  end
end