Module: Test::Spec::Rails::ShouldNotExpectations

Defined in:
lib/test/spec/rails/expectations.rb

Instance Method Summary collapse

Instance Method Details

#differ(*expected) ⇒ Object Also known as: change

Tests that the evaluation of the expression shouldn’t change

lambda { Norm.new }.should.not.differ('Norm.count')
lambda { Norm.new }.should.not.differ('Norm.count', 'Option.count')

norm = lambda { Norm.new }.should.not.differ('Norm.count')
norm.token.should.match /(\d\w){4}/


154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/test/spec/rails/expectations.rb', line 154

def differ(*expected)
  block_binding = @object.send(:binding)
  before = expected.map do |expression|
    eval(expression, block_binding)
  end
  
  block_result = @object.call
  
  expected.each_with_index do |expression, index|
    difference = eval(expression, block_binding) - before[index]
    error = "#{expression.inspect} changed by #{difference}, expected no change"
    test_case.assert_equal(0, difference, error)
  end
  
  block_result
  
end

#dom_equal(expected) ⇒ Object

Test that two HTML strings are not equivalent



174
175
176
# File 'lib/test/spec/rails/expectations.rb', line 174

def dom_equal(expected)
  test_case.assert_dom_not_equal expected, @object
end

#equal_list(expected) ⇒ Object

Tests if the array of records is not the same, order may vary



189
190
191
192
193
194
195
196
# File 'lib/test/spec/rails/expectations.rb', line 189

def equal_list(expected)
  message = "#{Helpers.inspect_records(@object)} has the same records as #{Helpers.inspect_records(expected)}"
  
  left = @object.map(&:id)
  right = expected.map(&:id)
  
  test_case.assert(left != right, message)
end

#equal_set(expected) ⇒ Object

Tests if the array of records is not the same, order may vary



179
180
181
182
183
184
185
186
# File 'lib/test/spec/rails/expectations.rb', line 179

def equal_set(expected)
  message = "#{Helpers.inspect_records(@object)} has the same records as #{Helpers.inspect_records(expected)}"
  
  left = @object.map(&:id).sort
  right = expected.map(&:id).sort
  
  test_case.assert(left != right, message)
end

#validateObject

Test that an object is not valid



143
144
145
# File 'lib/test/spec/rails/expectations.rb', line 143

def validate
  test_case.assert !@object.valid?
end

#validate_with(attribute, value) ⇒ Object

Tests if the model has errors on the attribute after validation for the presented value



199
200
201
202
203
204
205
206
207
208
# File 'lib/test/spec/rails/expectations.rb', line 199

def validate_with(attribute, value)
  message = "Expected errors on #{attribute.inspect} with value `#{value.inspect}' after validation"
  @object.send("#{attribute}=", value)
  @object.valid?
  if @object.errors[attribute].kind_of?(Array)
    test_case.assert(!@object.errors[attribute].empty?, message)
  else
    test_case.assert(!@object.errors.on(attribute).nil?, message)
  end
end