File: failure_messages_test.rb

package info (click to toggle)
ruby-mocha 3.0.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,652 kB
  • sloc: ruby: 12,324; javascript: 499; makefile: 14
file content (82 lines) | stat: -rw-r--r-- 2,414 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# frozen_string_literal: true

require File.expand_path('../acceptance_test_helper', __FILE__)
require 'mocha/ruby_version'

class FailureMessagesTest < Mocha::TestCase
  OBJECT_ADDRESS_PATTERN = '0x[0-9A-Fa-f]{1,12}'

  include AcceptanceTestHelper

  def setup
    setup_acceptance_test
  end

  def teardown
    teardown_acceptance_test
  end

  class Foo; end

  def test_should_display_class_name_when_expectation_was_on_class
    test_result = run_as_test do
      Foo.expects(:bar)
    end
    assert_match Regexp.new('FailureMessagesTest::Foo'), test_result.failures[0].message
  end

  def test_should_display_class_name_and_address_when_expectation_was_on_instance
    test_result = run_as_test do
      Foo.new.expects(:bar)
    end
    assert_match Regexp.new("#<FailureMessagesTest::Foo:#{OBJECT_ADDRESS_PATTERN}>"), test_result.failures[0].message
  end

  def test_should_display_class_name_and_any_instance_prefix_when_expectation_was_on_any_instance
    test_result = run_as_test do
      Foo.any_instance.expects(:bar)
    end
    assert_match Regexp.new('#<AnyInstance:FailureMessagesTest::Foo>'), test_result.failures[0].message
  end

  def test_should_display_mock_name_when_expectation_was_on_named_mock
    test_result = run_as_test do
      foo = mock('foo')
      foo.expects(:bar)
    end
    assert_match Regexp.new('#<Mock:foo>'), test_result.failures[0].message
  end

  def test_should_display_mock_address_when_expectation_was_on_unnamed_mock
    test_result = run_as_test do
      foo = mock
      foo.expects(:bar)
    end
    assert_match Regexp.new("#<Mock:#{OBJECT_ADDRESS_PATTERN}>"), test_result.failures[0].message
  end

  unless Mocha::RUBY_V34_PLUS
    def test_should_display_string_when_expectation_was_on_string
      test_result = run_as_test do
        'Foo'.dup.expects(:bar)
      end
      assert_match Regexp.new(%("Foo")), test_result.failures[0].message
    end
  end

  def test_should_display_that_block_was_expected
    test_result = run_as_test do
      foo = mock
      foo.expects(:bar).with_block_given
    end
    assert_match Regexp.new(' with block given$'), test_result.failures[0].message
  end

  def test_should_display_that_block_was_not_expected
    test_result = run_as_test do
      foo = mock
      foo.expects(:bar).with_no_block_given
    end
    assert_match Regexp.new(' with no block given$'), test_result.failures[0].message
  end
end