File: should_ignore_missing_test.rb

package info (click to toggle)
ruby-flexmock 3.0.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 840 kB
  • sloc: ruby: 7,598; makefile: 6
file content (84 lines) | stat: -rw-r--r-- 2,234 bytes parent folder | download | duplicates (5)
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
83
84
#!/usr/bin/env ruby

#---
# Copyright 2003-2013 by Jim Weirich (jim.weirich@gmail.com).
# All rights reserved.

# Permission is granted for use, copying, modification, distribution,
# and distribution of modified versions of this work as long as the
# above copyright notice is included.
#+++

require 'test_helper'

class TestShouldIgnoreMissing < Minitest::Test
  include FlexMock::Minitest

  def setup
    @mock = flexmock("mock")
  end

  def test_mocks_do_not_respond_to_undefined_methods
    assert !@mock.respond_to?(:unknown_foo)
  end

  def test_mocks_do_respond_to_defined_methods
    @mock.should_receive(:known_foo => :bar)
    assert @mock.respond_to?(:known_foo)
  end

  def test_mocks_do_respond_to_any_method_when_ignoring_missing
    @mock.should_ignore_missing
    assert @mock.respond_to?(:unknown_foo)
  end

  def test_should_ignore_missing_returns_mock
    result = @mock.should_ignore_missing
    assert_equal result, @mock
  end

  def test_ignored_methods_return_undefined
    @mock.should_ignore_missing
    assert_equal FlexMock.undefined, @mock.unknown_foo
    @mock.unknown_foo.bar.baz.bleep
  end

  def test_undefined_mocking_with_arguments
    @mock.should_ignore_missing
    assert_equal FlexMock.undefined, @mock.xyzzy(1,:two,"three")
  end

  def test_method_chains_with_undefined_are_self_preserving
    @mock.should_ignore_missing
    assert_equal FlexMock.undefined, @mock.a.b.c.d.e.f(1).g.h.i.j
  end

  def test_method_proc_raises_error_on_unknown
    assert_raises(NameError) {
      @mock.method(:unknown_foo)
    }
  end

  def test_method_returns_callable_proc
    @mock.should_receive(:known_foo).once
    method_proc = @mock.method(:known_foo)
    refute_nil method_proc
    method_proc.call([])
  end

  def test_not_calling_method_proc_will_fail_count_constraints
    @mock.should_receive(:known_foo).once
    method_proc = @mock.method(:known_foo)
    refute_nil method_proc
    assert_raises assertion_failed_error do
      flexmock_teardown
    end
  end

  def test_method_returns_do_nothing_proc_for_missing_methods
    @mock.should_ignore_missing
    method_proc = @mock.method(:plugh)
    refute_nil method_proc
    assert_equal FlexMock.undefined, method_proc.call
  end
end