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
|
Feature: Equality matchers
Ruby exposes several different methods for handling equality:
a.equal?(b) # object identity - a and b refer to the same object
a.eql?(b) # object equivalence - a and b have the same value
a == b # object equivalence - a and b have the same value with type conversions
Note that these descriptions are guidelines but are not forced by the language. Any object
can implement any of these methods with its own semantics.
rspec-expectations ships with matchers that align with each of these methods:
```ruby
expect(a).to equal(b) # passes if a.equal?(b)
expect(a).to eql(b) # passes if a.eql?(b)
expect(a).to be == b # passes if a == b
```
It also ships with two matchers that have more of a DSL feel to them:
```ruby
expect(a).to be(b) # passes if a.equal?(b)
expect(a).to eq(b) # passes if a == b
```
Scenario: Compare using eq (==)
Given a file named "compare_using_eq.rb" with:
"""ruby
RSpec.describe "a string" do
it "is equal to another string of the same value" do
expect("this string").to eq("this string")
end
it "is not equal to another string of a different value" do
expect("this string").not_to eq("a different string")
end
end
RSpec.describe "an integer" do
it "is equal to a float of the same value" do
expect(5).to eq(5.0)
end
end
"""
When I run `rspec compare_using_eq.rb`
Then the output should contain "3 examples, 0 failures"
Scenario: Compare using ==
Given a file named "compare_using_==.rb" with:
"""ruby
RSpec.describe "a string" do
it "is equal to another string of the same value" do
expect("this string").to be == "this string"
end
it "is not equal to another string of a different value" do
expect("this string").not_to be == "a different string"
end
end
RSpec.describe "an integer" do
it "is equal to a float of the same value" do
expect(5).to be == 5.0
end
end
"""
When I run `rspec compare_using_==.rb`
Then the output should contain "3 examples, 0 failures"
Scenario: Compare using eql (eql?)
Given a file named "compare_using_eql.rb" with:
"""ruby
RSpec.describe "an integer" do
it "is equal to another integer of the same value" do
expect(5).to eql(5)
end
it "is not equal to another integer of a different value" do
expect(5).not_to eql(6)
end
it "is not equal to a float of the same value" do
expect(5).not_to eql(5.0)
end
end
"""
When I run `rspec compare_using_eql.rb`
Then the output should contain "3 examples, 0 failures"
Scenario: Compare using equal (equal?)
Given a file named "compare_using_equal.rb" with:
"""ruby
RSpec.describe "a string" do
it "is equal to itself" do
string = "this string"
expect(string).to equal(string)
end
it "is not equal to another string of the same value" do
expect("this string").not_to equal("this string")
end
it "is not equal to another string of a different value" do
expect("this string").not_to equal("a different string")
end
end
"""
When I run `rspec compare_using_equal.rb`
Then the output should contain "3 examples, 0 failures"
Scenario: Compare using be (equal?)
Given a file named "compare_using_be.rb" with:
"""ruby
RSpec.describe "a string" do
it "is equal to itself" do
string = "this string"
expect(string).to be(string)
end
it "is not equal to another string of the same value" do
expect("this string").not_to be("this string")
end
it "is not equal to another string of a different value" do
expect("this string").not_to be("a different string")
end
end
"""
When I run `rspec compare_using_be.rb`
Then the output should contain "3 examples, 0 failures"
|