File: respond_to.feature

package info (click to toggle)
ruby-rspec-expectations 2.14.2-1~bpo70%2B1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy-backports
  • size: 920 kB
  • sloc: ruby: 8,202; makefile: 4
file content (84 lines) | stat: -rw-r--r-- 3,434 bytes parent folder | download | duplicates (2)
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
Feature: respond_to matcher

  Use the respond_to matcher to specify details of an object's interface.  In
  its most basic form:

    ```ruby
    obj.should respond_to(:foo) # pass if obj.respond_to?(:foo)
    ```

  You can specify that an object responds to multiple messages in a single
  statement with multiple arguments passed to the matcher:

    ```ruby
    obj.should respond_to(:foo, :bar) # passes if obj.respond_to?(:foo) && obj.respond_to?(:bar)
    ```

  If the number of arguments accepted by the method is important to you,
  you can specify that as well:

    ```ruby
    obj.should respond_to(:foo).with(1).argument
    obj.should respond_to(:bar).with(2).arguments
    ```

  Note that this matcher relies entirely upon #respond_to?.  If an object
  dynamically responds to a message via #method_missing, but does not indicate
  this via #respond_to?, then this matcher will give you false results.

  Scenario: basic usage
    Given a file named "respond_to_matcher_spec.rb" with:
      """ruby
      describe "a string" do
        it { should respond_to(:length) }
        it { should respond_to(:hash, :class, :to_s) }
        it { should_not respond_to(:to_model) }
        it { should_not respond_to(:compact, :flatten) }

        # deliberate failures
        it { should respond_to(:to_model) }
        it { should respond_to(:compact, :flatten) }
        it { should_not respond_to(:length) }
        it { should_not respond_to(:hash, :class, :to_s) }

        # mixed examples--String responds to :length but not :flatten
        # both specs should fail
        it { should respond_to(:length, :flatten) }
        it { should_not respond_to(:length, :flatten) }
      end
      """
    When I run `rspec respond_to_matcher_spec.rb`
    Then the output should contain all of these:
      | 10 examples, 6 failures                                    |
      | expected "a string" to respond to :to_model                |
      | expected "a string" to respond to :compact, :flatten       |
      | expected "a string" not to respond to :length              |
      | expected "a string" not to respond to :hash, :class, :to_s |
      | expected "a string" to respond to :flatten                 |
      | expected "a string" not to respond to :length              |

  Scenario: specify arguments
    Given a file named "respond_to_matcher_argument_checking_spec.rb" with:
      """ruby
      describe 7 do
        it { should respond_to(:zero?).with(0).arguments }
        it { should_not respond_to(:zero?).with(1).argument }

        it { should respond_to(:between?).with(2).arguments }
        it { should_not respond_to(:between?).with(7).arguments }

        # deliberate failures
        it { should respond_to(:zero?).with(1).argument }
        it { should_not respond_to(:zero?).with(0).arguments }

        it { should respond_to(:between?).with(7).arguments }
        it { should_not respond_to(:between?).with(2).arguments }
      end
      """
    When I run `rspec respond_to_matcher_argument_checking_spec.rb`
    Then the output should contain all of these:
      | 8 examples, 4 failures                                  |
      | expected 7 to respond to :zero? with 1 argument         |
      | expected 7 not to respond to :zero? with 0 arguments    |
      | expected 7 to respond to :between? with 7 arguments     |
      | expected 7 not to respond to :between? with 2 arguments |