File: README.md

package info (click to toggle)
ruby-rspec-mocks 2.14.5-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 868 kB
  • ctags: 725
  • sloc: ruby: 8,227; makefile: 4
file content (73 lines) | stat: -rw-r--r-- 2,295 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
### Stub return values

    # create a double
    obj = double()

    # specify a return value using `:expect` syntax
    allow(obj).to receive(:message) { :value }
    allow(obj).to receive(:message).and_return(:value)

    # specify a return value using `:should` syntax
    obj.stub(:message) { :value }
    obj.stub(:message => :value)
    obj.stub(:message).and_return(:value)

These forms are somewhat interchangeable. The difference is that the
block contents are evaluated lazily when the `obj` receives the
`message` message, whereas the others are evaluated as they are read.

### Fake implementation

    allow(obj).to receive(:message) do |arg1, arg2|
      # set expectations about the args in this block
      # and/or return  value
    end

    obj.stub(:message) do |arg1, arg2|
      # set expectations about the args in this block
      # and/or return a value
    end

### Raising/Throwing

    allow(obj).to receive(:message).and_raise("this error")
    allow(obj).to receive(:message).and_throw(:this_symbol)

    obj.stub(:message).and_raise("this error")
    obj.stub(:message).and_throw(:this_symbol)

You can also use the block format:

    allow(obj).to receive(:message) { raise "this error" }
    allow(obj).to receive(:message) { throw :this_symbol }

    obj.stub(:message) { raise "this error" }
    obj.stub(:message) { throw :this_symbol }

### Argument constraints

#### Explicit arguments

    allow(obj).to receive(:message).with('an argument') { ... }

    obj.stub(:message).with('an argument') { ... }
    obj.stub(:message).with('more_than', 'one_argument') { ... }

#### Argument matchers

    allow(obj).to receive(:message).with(anything()) { ... }
    allow(obj).to receive(:message).with(an_instance_of(Money)) { ... }
    allow(obj).to receive(:message).with(hash_including(:a => 'b')) { ... }
    allow(obj).to receive(:message).with(array_including(1,2,3)) { ... }
    # or
    allow(obj).to receive(:message).with(array_including([1,2,3])) { ... }

    obj.stub(:message).with(anything()) { ... }
    obj.stub(:message).with(an_instance_of(Money)) { ... }
    obj.stub(:message).with(hash_including(:a => 'b')) { ... }

#### Regular expressions

    allow(obj).to receive(:message).with(/abc/) { ... }

    obj.stub(:message).with(/abc/) { ... }