class AssertDifferenceTest

Public Instance Methods

decrement() click to toggle source
# File activesupport/test/test_case_test.rb, line 13
def decrement
  self.num -= 1
end
increment() click to toggle source
# File activesupport/test/test_case_test.rb, line 9
def increment
  self.num += 1
end
setup() click to toggle source
# File activesupport/test/test_case_test.rb, line 6
def setup
  @object = Class.new do
    attr_accessor :num
    def increment
      self.num += 1
    end

    def decrement
      self.num -= 1
    end
  end.new
  @object.num = 0
end
test_arbitrary_expression() click to toggle source
# File activesupport/test/test_case_test.rb, line 75
def test_arbitrary_expression
  assert_difference "@object.num + 1", +2 do
    @object.increment
    @object.increment
  end
end
test_array_of_expressions() click to toggle source
# File activesupport/test/test_case_test.rb, line 95
def test_array_of_expressions
  assert_difference [ "@object.num", "@object.num + 1" ], +1 do
    @object.increment
  end
end
test_array_of_expressions_identify_failure() click to toggle source
# File activesupport/test/test_case_test.rb, line 101
def test_array_of_expressions_identify_failure
  assert_raises(Minitest::Assertion) do
    assert_difference ["@object.num", "1 + 1"] do
      @object.increment
    end
  end
end
test_array_of_expressions_identify_failure_when_message_provided() click to toggle source
# File activesupport/test/test_case_test.rb, line 109
def test_array_of_expressions_identify_failure_when_message_provided
  assert_raises(Minitest::Assertion) do
    assert_difference ["@object.num", "1 + 1"], 1, "something went wrong" do
      @object.increment
    end
  end
end
test_assert_changes_pass() click to toggle source
# File activesupport/test/test_case_test.rb, line 117
def test_assert_changes_pass
  assert_changes "@object.num" do
    @object.increment
  end
end
test_assert_changes_pass_with_lambda() click to toggle source
# File activesupport/test/test_case_test.rb, line 123
def test_assert_changes_pass_with_lambda
  assert_changes -> { @object.num } do
    @object.increment
  end
end
test_assert_changes_with_from_and_to_options_and_wrong_to_value() click to toggle source
# File activesupport/test/test_case_test.rb, line 172
def test_assert_changes_with_from_and_to_options_and_wrong_to_value
  assert_raises Minitest::Assertion do
    assert_changes "@object.num", from: 0, to: 2 do
      @object.increment
    end
  end
end
test_assert_changes_with_from_option() click to toggle source
# File activesupport/test/test_case_test.rb, line 129
def test_assert_changes_with_from_option
  assert_changes "@object.num", from: 0 do
    @object.increment
  end
end
test_assert_changes_with_from_option_and_to_option() click to toggle source
# File activesupport/test/test_case_test.rb, line 166
def test_assert_changes_with_from_option_and_to_option
  assert_changes "@object.num", from: 0, to: 1 do
    @object.increment
  end
end
test_assert_changes_with_from_option_with_nil() click to toggle source
# File activesupport/test/test_case_test.rb, line 143
def test_assert_changes_with_from_option_with_nil
  error = assert_raises Minitest::Assertion do
    assert_changes "@object.num", from: nil do
      @object.increment
    end
  end
  assert_equal "\"@object.num\" isn't nil", error.message
end
test_assert_changes_with_from_option_with_wrong_value() click to toggle source
# File activesupport/test/test_case_test.rb, line 135
def test_assert_changes_with_from_option_with_wrong_value
  assert_raises Minitest::Assertion do
    assert_changes "@object.num", from: -1 do
      @object.increment
    end
  end
end
test_assert_changes_with_message() click to toggle source
# File activesupport/test/test_case_test.rb, line 216
def test_assert_changes_with_message
  error = assert_raises Minitest::Assertion do
    assert_changes "@object.num", "@object.num should 1", to: 1 do
    end
  end

  assert_equal "@object.num should 1.\n\"@object.num\" didn't change to 1", error.message
end
test_assert_changes_with_to_and_case_operator() click to toggle source
# File activesupport/test/test_case_test.rb, line 200
def test_assert_changes_with_to_and_case_operator
  token = nil

  assert_changes "token", to: /\w{32}/ do
    token = SecureRandom.hex
  end
end
test_assert_changes_with_to_and_from_and_case_operator() click to toggle source
# File activesupport/test/test_case_test.rb, line 208
def test_assert_changes_with_to_and_from_and_case_operator
  token = SecureRandom.hex

  assert_changes "token", from: /\w{32}/, to: /\w{32}/ do
    token = SecureRandom.hex
  end
end
test_assert_changes_with_to_option() click to toggle source
# File activesupport/test/test_case_test.rb, line 152
def test_assert_changes_with_to_option
  assert_changes "@object.num", to: 1 do
    @object.increment
  end
end
test_assert_changes_with_wrong_to_option() click to toggle source
# File activesupport/test/test_case_test.rb, line 158
def test_assert_changes_with_wrong_to_option
  assert_raises Minitest::Assertion do
    assert_changes "@object.num", to: 2 do
      @object.increment
    end
  end
end
test_assert_changes_works_with_any_object() click to toggle source
# File activesupport/test/test_case_test.rb, line 180
def test_assert_changes_works_with_any_object
  retval = silence_warnings do
    assert_changes :@new_object, from: nil, to: 42 do
      @new_object = 42
    end
  end

  assert_equal 42, retval
end
test_assert_changes_works_with_nil() click to toggle source
# File activesupport/test/test_case_test.rb, line 190
def test_assert_changes_works_with_nil
  oldval = @object

  retval = assert_changes :@object, from: oldval, to: nil do
    @object = nil
  end

  assert_nil retval
end
test_assert_difference() click to toggle source
# File activesupport/test/test_case_test.rb, line 55
def test_assert_difference
  assert_difference "@object.num", +1 do
    @object.increment
  end
end
test_assert_difference_retval() click to toggle source
# File activesupport/test/test_case_test.rb, line 61
def test_assert_difference_retval
  incremented = assert_difference "@object.num", +1 do
    @object.increment
  end

  assert_equal incremented, 1
end
test_assert_difference_with_implicit_difference() click to toggle source
# File activesupport/test/test_case_test.rb, line 69
def test_assert_difference_with_implicit_difference
  assert_difference "@object.num" do
    @object.increment
  end
end
test_assert_no_changes_pass() click to toggle source
# File activesupport/test/test_case_test.rb, line 225
def test_assert_no_changes_pass
  assert_no_changes "@object.num" do
    # ...
  end
end
test_assert_no_changes_with_message() click to toggle source
# File activesupport/test/test_case_test.rb, line 231
def test_assert_no_changes_with_message
  error = assert_raises Minitest::Assertion do
    assert_no_changes "@object.num", "@object.num should not change" do
      @object.increment
    end
  end

  assert_equal "@object.num should not change.\n\"@object.num\" did change to 1.\nExpected: 0\n  Actual: 1", error.message
end
test_assert_no_difference_fail() click to toggle source
# File activesupport/test/test_case_test.rb, line 37
def test_assert_no_difference_fail
  error = assert_raises(Minitest::Assertion) do
    assert_no_difference "@object.num" do
      @object.increment
    end
  end
  assert_equal "\"@object.num\" didn't change by 0.\nExpected: 0\n  Actual: 1", error.message
end
test_assert_no_difference_pass() click to toggle source
# File activesupport/test/test_case_test.rb, line 31
def test_assert_no_difference_pass
  assert_no_difference "@object.num" do
    # ...
  end
end
test_assert_no_difference_with_message_fail() click to toggle source
# File activesupport/test/test_case_test.rb, line 46
def test_assert_no_difference_with_message_fail
  error = assert_raises(Minitest::Assertion) do
    assert_no_difference "@object.num", "Object Changed" do
      @object.increment
    end
  end
  assert_equal "Object Changed.\n\"@object.num\" didn't change by 0.\nExpected: 0\n  Actual: 1", error.message
end
test_assert_not() click to toggle source
# File activesupport/test/test_case_test.rb, line 20
def test_assert_not
  assert_equal true, assert_not(nil)
  assert_equal true, assert_not(false)

  e = assert_raises(Minitest::Assertion) { assert_not true }
  assert_equal "Expected true to be nil or false", e.message

  e = assert_raises(Minitest::Assertion) { assert_not true, "custom" }
  assert_equal "custom", e.message
end
test_expression_is_evaluated_in_the_appropriate_scope() click to toggle source
# File activesupport/test/test_case_test.rb, line 88
def test_expression_is_evaluated_in_the_appropriate_scope
  silence_warnings do
    local_scope = local_scope = "foo"
    assert_difference("local_scope; @object.num") { @object.increment }
  end
end
test_negative_differences() click to toggle source
# File activesupport/test/test_case_test.rb, line 82
def test_negative_differences
  assert_difference "@object.num", -1 do
    @object.decrement
  end
end