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 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
|
Feature: operator matchers
RSpec provides a number of matchers that are based on Ruby's built-in
operators. These pretty much work like you expect. For example, each of these
pass:
```ruby
7.should == 7
[1, 2, 3].should == [1, 2, 3]
"this is a string".should =~ /^this/
"this is a string".should_not =~ /^that/
String.should === "this is a string"
```
You can also use comparison operators combined with the "be" matcher like
this:
```ruby
37.should be < 100
37.should be <= 38
37.should be >= 2
37.should be > 7
```
RSpec also provides a `=~` matcher for arrays that disregards differences in
the ording between the actual and expected array. For example:
```ruby
[1, 2, 3].should =~ [2, 3, 1] # pass
[:a, :c, :b].should =~ [:a, :c] # fail
```
Scenario: numeric operator matchers
Given a file named "numeric_operator_matchers_spec.rb" with:
"""ruby
describe 18 do
it { should == 18 }
it { should be < 20 }
it { should be > 15 }
it { should be <= 19 }
it { should be >= 17 }
it { should_not == 28 }
# deliberate failures
it { should == 28 }
it { should be < 15 }
it { should be > 20 }
it { should be <= 17 }
it { should be >= 19 }
it { should_not == 18 }
end
"""
When I run `rspec numeric_operator_matchers_spec.rb`
Then the output should contain "12 examples, 6 failures"
And the output should contain:
"""
Failure/Error: it { should == 28 }
expected: 28
got: 18 (using ==)
"""
And the output should contain:
"""
Failure/Error: it { should be < 15 }
expected: < 15
got: 18
"""
And the output should contain:
"""
Failure/Error: it { should be > 20 }
expected: > 20
got: 18
"""
And the output should contain:
"""
Failure/Error: it { should be <= 17 }
expected: <= 17
got: 18
"""
And the output should contain:
"""
Failure/Error: it { should be >= 19 }
expected: >= 19
got: 18
"""
And the output should contain:
"""
Failure/Error: it { should_not == 18 }
expected not: == 18
got: 18
"""
Scenario: string operator matchers
Given a file named "string_operator_matchers_spec.rb" with:
"""ruby
describe "Strawberry" do
it { should == "Strawberry" }
it { should be < "Tomato" }
it { should be > "Apple" }
it { should be <= "Turnip" }
it { should be >= "Banana" }
it { should =~ /berry/ }
it { should_not == "Peach" }
it { should_not =~ /apple/ }
it "reports that it is a string using ===" do
String.should === subject
end
# deliberate failures
it { should == "Peach" }
it { should be < "Cranberry" }
it { should be > "Zuchini" }
it { should be <= "Potato" }
it { should be >= "Tomato" }
it { should =~ /apple/ }
it { should_not == "Strawberry" }
it { should_not =~ /berry/ }
it "fails a spec asserting that it is a symbol" do
Symbol.should === subject
end
end
"""
When I run `rspec string_operator_matchers_spec.rb`
Then the output should contain "18 examples, 9 failures"
And the output should contain:
"""
Failure/Error: it { should == "Peach" }
expected: "Peach"
got: "Strawberry" (using ==)
"""
And the output should contain:
"""
Failure/Error: it { should be < "Cranberry" }
expected: < "Cranberry"
got: "Strawberry"
"""
And the output should contain:
"""
Failure/Error: it { should be > "Zuchini" }
expected: > "Zuchini"
got: "Strawberry"
"""
And the output should contain:
"""
Failure/Error: it { should be <= "Potato" }
expected: <= "Potato"
got: "Strawberry"
"""
And the output should contain:
"""
Failure/Error: it { should be >= "Tomato" }
expected: >= "Tomato"
got: "Strawberry"
"""
And the output should contain:
"""
Failure/Error: it { should =~ /apple/ }
expected: /apple/
got: "Strawberry" (using =~)
"""
And the output should contain:
"""
Failure/Error: it { should_not == "Strawberry" }
expected not: == "Strawberry"
got: "Strawberry"
"""
And the output should contain:
"""
Failure/Error: it { should_not =~ /berry/ }
expected not: =~ /berry/
got: "Strawberry"
"""
And the output should contain:
"""
Failure/Error: Symbol.should === subject
expected: "Strawberry"
got: Symbol (using ===)
"""
Scenario: array operator matchers
Given a file named "array_operator_matchers_spec.rb" with:
"""ruby
describe [1, 2, 3] do
it { should == [1, 2, 3] }
it { should_not == [1, 3, 2] }
it { should =~ [1, 2, 3] }
it { should =~ [1, 3, 2] }
it { should =~ [2, 1, 3] }
it { should =~ [2, 3, 1] }
it { should =~ [3, 1, 2] }
it { should =~ [3, 2, 1] }
# deliberate failures
it { should_not == [1, 2, 3] }
it { should == [1, 3, 2] }
it { should =~ [1, 2, 1] }
end
"""
When I run `rspec array_operator_matchers_spec.rb`
Then the output should contain "11 examples, 3 failures"
And the output should contain:
"""
Failure/Error: it { should_not == [1, 2, 3] }
expected not: == [1, 2, 3]
got: [1, 2, 3]
"""
And the output should contain:
"""
Failure/Error: it { should == [1, 3, 2] }
expected: [1, 3, 2]
got: [1, 2, 3] (using ==)
"""
And the output should contain:
"""
Failure/Error: it { should =~ [1, 2, 1] }
expected collection contained: [1, 1, 2]
actual collection contained: [1, 2, 3]
the missing elements were: [1]
the extra elements were: [3]
"""
|