Permalink
Newer
Older
100644 86 lines (75 sloc) 2.63 KB
1
Feature: General matchers
2
3
The `anything`, `any_args`, and `no_args` matchers can be used to require the method
4
to have arguments (or not) without constraining the details of the argument, such as its
5
type, pattern or value. The `anything` matcher only reflects a single argument, while
6
the `any_args` matcher matches any arity.
7
8
Scenario: anything argument matcher
9
Given a file named "stub_anything_args_spec.rb" with:
11
describe "stubbed anything() args spec" do
12
it "works" do
13
object = Object.new
14
object.stub(:foo).with(anything) do
15
"anything"
16
end
17
18
object.foo(1).should eq("anything")
19
object.foo(:that).should eq("anything")
20
end
21
end
22
"""
23
When I run `rspec stub_anything_args_spec.rb`
24
Then the output should contain "1 example, 0 failures"
25
26
Scenario: any_args argument matcher
27
Given a file named "stub_any_args_spec.rb" with:
29
describe "stubbed any_args() args spec" do
30
it "works" do
31
object = Object.new
32
object.stub(:foo).with(any_args) do
33
"anything"
34
end
35
36
object.foo(1).should eq("anything")
37
object.foo(:that).should eq("anything")
38
object.foo.should eq("anything")
39
end
40
end
41
"""
42
When I run `rspec stub_any_args_spec.rb`
43
Then the output should contain "1 example, 0 failures"
44
45
Scenario: no_args argument matcher
46
Given a file named "stub_no_args_spec.rb" with:
48
describe "stubbed no_args() args spec" do
49
it "works for no args" do
50
object = Object.new
51
object.stub(:foo).with(no_args) do
52
"nothing"
53
end
54
object.stub(:foo).with(anything) do
55
"something"
56
end
57
58
object.foo(:that).should eq("something")
59
object.foo.should eq("nothing")
60
end
61
end
62
"""
63
When I run `rspec stub_no_args_spec.rb`
64
Then the output should contain "1 example, 0 failures"
65
66
Scenario: no_args argument matcher for expectations
67
Given a file named "stub_no_args_expectations_spec.rb" with:
69
describe "stubbed no_args() args spec for expectations" do
70
it "works for no args" do
71
object = Object.new
72
object.should_receive(:foo).with(no_args)
73
74
object.foo
75
end
76
it "fails for args" do
77
object = Object.new
78
object.should_receive(:foo).with(no_args)
79
80
object.foo(:bar)
81
end
82
end
83
"""
84
When I run `rspec stub_no_args_expectations_spec.rb`
85
Then the output should contain "2 examples, 1 failure"