| 1 |
RSpec.describe "#have_attributes matcher" do |
| 2 |
include RSpec::Support::Spec::DiffHelpers |
| 3 |
|
| 4 |
Person = Struct.new(:name, :age) |
| 5 |
|
| 6 |
class Person |
| 7 |
def parent(parent_name) |
| 8 |
@parent = parent_name |
| 9 |
end |
| 10 |
end |
| 11 |
|
| 12 |
|
| 13 |
class DynamicAttributes |
| 14 |
def initialize(attributes) |
| 15 |
@attributes = attributes |
| 16 |
end |
| 17 |
|
| 18 |
def method_missing(name, *args, &block) |
| 19 |
@attributes[name] || super |
| 20 |
end |
| 21 |
|
| 22 |
def respond_to?(method_name) |
| 23 |
@attributes.keys.include?(method_name) || super |
| 24 |
end |
| 25 |
end |
| 26 |
|
| 27 |
let(:wrong_name) { "Wrong Name" } |
| 28 |
let(:wrong_age) { 11 } |
| 29 |
|
| 30 |
let(:correct_name) { "Correct name" } |
| 31 |
let(:correct_age) { 33 } |
| 32 |
|
| 33 |
let(:person) { Person.new(correct_name, correct_age) } |
| 34 |
|
| 35 |
it "is diffable" do |
| 36 |
expect(have_attributes(:age => correct_age)).to be_diffable |
| 37 |
end |
| 38 |
|
| 39 |
describe "expect(...).to have_attributes(with_one_attribute)" do |
| 40 |
it_behaves_like "an RSpec value matcher", :valid_value => Person.new("Correct name", 33), :invalid_value => Person.new("Wrong Name", 11) do |
| 41 |
let(:matcher) { have_attributes(:name => "Correct name") } |
| 42 |
end |
| 43 |
|
| 44 |
it "passes if target has the provided attributes" do |
| 45 |
expect(person).to have_attributes(:name => correct_name) |
| 46 |
end |
| 47 |
|
| 48 |
it "passes if target responds to :sym but does not implement method" do |
| 49 |
expect(DynamicAttributes.new(:name => "value")).to have_attributes(:name => "value") |
| 50 |
end |
| 51 |
|
| 52 |
it "fails if target does not have any of the expected attributes" do |
| 53 |
expect { |
| 54 |
expect(person).to have_attributes(:name => wrong_name) |
| 55 |
}.to fail_including("expected #{object_inspect person} to have attributes #{hash_inspect :name => wrong_name} but had attributes #{hash_inspect :name => correct_name }") |
| 56 |
end |
| 57 |
|
| 58 |
it "fails with correct message if object manipulates its data" do |
| 59 |
counter = Class.new do |
| 60 |
def initialize; @count = 1; end |
| 61 |
def count |
| 62 |
@count += 1 |
| 63 |
end |
| 64 |
end.new |
| 65 |
expect { |
| 66 |
expect(counter).to have_attributes(:count => 1) |
| 67 |
}.to fail_including("to have attributes #{hash_inspect :count => 1} but had attributes #{hash_inspect :count => 2 }") |
| 68 |
end |
| 69 |
|
| 70 |
it 'diffs the attributes received with those expected' do |
| 71 |
allow(RSpec::Matchers.configuration).to receive_messages(:color? => false) |
| 72 |
|
| 73 |
expected_diff = dedent(<<-EOS) |
| 74 |
|@@ #{one_line_header} @@ |
| 75 |
|-:name => "Wrong Name", |
| 76 |
|+:name => "Correct name", |
| 77 |
EOS |
| 78 |
|
| 79 |
expect { |
| 80 |
expect(person).to have_attributes(:name => wrong_name) |
| 81 |
}.to fail_including(expected_diff) |
| 82 |
end |
| 83 |
|
| 84 |
it "fails if target does not responds to any of the attributes" do |
| 85 |
expect { |
| 86 |
expect(person).to have_attributes(:color => 'red') |
| 87 |
}.to fail_including("expected #{object_inspect person} to respond to :color") |
| 88 |
end |
| 89 |
|
| 90 |
it "doesn't produce a diff if the target fails the respond to check" do |
| 91 |
expect { |
| 92 |
expect(person).to have_attributes(:color => 'red') |
| 93 |
}.to fail_with(a_string_excluding "Diff") |
| 94 |
end |
| 95 |
|
| 96 |
it "fails if target responds to the attribute but requires arguments" do |
| 97 |
expect { |
| 98 |
expect(person).to have_attributes(:parent => 'Billy') |
| 99 |
}.to fail_including("expected #{object_inspect person} to respond to :parent with 0 arguments") |
| 100 |
end |
| 101 |
|
| 102 |
describe "expect(...).to have_attributes(key => matcher)" do |
| 103 |
it "passes when the matchers match" do |
| 104 |
expect(person).to have_attributes(:age => (a_value > 30)) |
| 105 |
end |
| 106 |
|
| 107 |
it 'provides a description' do |
| 108 |
description = have_attributes(:age => (a_value > 30)).description |
| 109 |
expect(description).to eq( |
| 110 |
if RUBY_VERSION.to_f > 3.3 |
| 111 |
"have attributes {age: (a value > 30)}" |
| 112 |
else |
| 113 |
"have attributes {:age => (a value > 30)}" |
| 114 |
end |
| 115 |
) |
| 116 |
end |
| 117 |
|
| 118 |
it "fails with a clear message when the matcher does not match" do |
| 119 |
expect { |
| 120 |
expect(person).to have_attributes(:age => (a_value < 10)) |
| 121 |
}.to fail_including( |
| 122 |
if RUBY_VERSION.to_f > 3.3 |
| 123 |
"expected #{object_inspect person} to have attributes {age: (a value < 10)}" |
| 124 |
else |
| 125 |
"expected #{object_inspect person} to have attributes {:age => (a value < 10)}" |
| 126 |
end |
| 127 |
) |
| 128 |
end |
| 129 |
end |
| 130 |
end |
| 131 |
|
| 132 |
describe "expect(...).to_not have_attributes(with_one_attribute)" do |
| 133 |
it "passes if target does not have any of the expected attributes" do |
| 134 |
expect(person).to_not have_attributes(:age => wrong_age) |
| 135 |
end |
| 136 |
|
| 137 |
it "fails if target has all of the expected attributes" do |
| 138 |
expect { |
| 139 |
expect(person).to_not have_attributes(:age => correct_age) |
| 140 |
}.to fail_including("expected #{object_inspect person} not to have attributes #{hash_inspect :age => correct_age}") |
| 141 |
end |
| 142 |
|
| 143 |
it "doesn't produce a diff" do |
| 144 |
expect { |
| 145 |
expect(person).to_not have_attributes(:age => correct_age) |
| 146 |
}.to fail_with(a_string_excluding "Diff") |
| 147 |
end |
| 148 |
|
| 149 |
it "fails if target does not responds to any of the attributes" do |
| 150 |
expect { |
| 151 |
expect(person).to_not have_attributes(:color => 'red') |
| 152 |
}.to fail_including("expected #{object_inspect person} to respond to :color") |
| 153 |
end |
| 154 |
|
| 155 |
it "fails if target responds to the attribute but requires arguments" do |
| 156 |
expect { |
| 157 |
expect(person).to_not have_attributes(:parent => 'Billy') |
| 158 |
}.to fail_including("expected #{object_inspect person} to respond to :parent with 0 arguments") |
| 159 |
end |
| 160 |
end |
| 161 |
|
| 162 |
describe "expect(...).to have_attributes(with_multiple_attributes)" do |
| 163 |
it_behaves_like "an RSpec value matcher", :valid_value => Person.new("Correct name", 33), :invalid_value => Person.new("Wrong Name", 11) do |
| 164 |
let(:matcher) { have_attributes(:name => "Correct name", :age => 33) } |
| 165 |
end |
| 166 |
|
| 167 |
it "passes if target has the provided attributes" do |
| 168 |
expect(person).to have_attributes(:name => correct_name, :age => correct_age) |
| 169 |
end |
| 170 |
|
| 171 |
it "fails if target does not have any of the expected attributes" do |
| 172 |
expect { |
| 173 |
expect(person).to have_attributes(:name => correct_name, :age => wrong_age) |
| 174 |
}.to fail_with(%r|expected #{object_inspect person} to have attributes #{hash_inspect_for_regex :age => wrong_age, :name => correct_name }|) |
| 175 |
end |
| 176 |
|
| 177 |
it 'diffs the attributes received with those expected' do |
| 178 |
allow(RSpec::Matchers.configuration).to receive_messages(:color? => false) |
| 179 |
|
| 180 |
expected_diff = dedent(<<-EOS) |
| 181 |
|@@ #{one_line_header(3)} @@ |
| 182 |
|-:age => 11, |
| 183 |
|+:age => 33, |
| 184 |
EOS |
| 185 |
expected_diff << "\n :name => \"Correct name\",\n" if Diff::LCS::VERSION.to_f < 1.4 |
| 186 |
|
| 187 |
expect { |
| 188 |
expect(person).to have_attributes(:name => correct_name, :age => wrong_age) |
| 189 |
}.to fail_including(expected_diff) |
| 190 |
end |
| 191 |
|
| 192 |
it "fails if target does not responds to any of the attributes" do |
| 193 |
expect { |
| 194 |
expect(person).to have_attributes(:name => correct_name, :color => 'red') |
| 195 |
}.to fail_including("expected #{object_inspect person} to respond to :color") |
| 196 |
end |
| 197 |
|
| 198 |
it "fails if target responds to the attribute but requires arguments" do |
| 199 |
expect { |
| 200 |
expect(person).to have_attributes(:name => correct_name, :parent => 'Billy') |
| 201 |
}.to fail_including("expected #{object_inspect person} to respond to :parent with 0 arguments") |
| 202 |
end |
| 203 |
end |
| 204 |
|
| 205 |
describe "expect(...).to_not have_attributes(with_multiple_attributes)" do |
| 206 |
it "passes if target has none of the expected attributes" do |
| 207 |
expect(person).to_not have_attributes(:name => wrong_name, :age => wrong_age) |
| 208 |
end |
| 209 |
|
| 210 |
it "fails if target has any of the expected attributes" do |
| 211 |
expect { |
| 212 |
expect(person).to_not have_attributes(:name => wrong_name, :age => correct_age) |
| 213 |
}.to fail_with(%r|expected #{object_inspect person} not to have attributes #{hash_inspect_for_regex :age => correct_age, :name => wrong_name }|) |
| 214 |
end |
| 215 |
|
| 216 |
it "fails if target has all of the expected attributes" do |
| 217 |
expect { |
| 218 |
expect(person).to_not have_attributes(:name => correct_name, :age => correct_age) |
| 219 |
}.to fail_with(%r|expected #{object_inspect person} not to have attributes #{hash_inspect_for_regex :age => correct_age, :name => correct_name }|) |
| 220 |
end |
| 221 |
|
| 222 |
it "fails if target does not responds to any of the attributes" do |
| 223 |
expect { |
| 224 |
expect(person).to_not have_attributes(:name => correct_name, :color => 'red') |
| 225 |
}.to fail_including("expected #{object_inspect person} to respond to :color") |
| 226 |
end |
| 227 |
|
| 228 |
it "fails if target responds to the attribute but requires arguments" do |
| 229 |
expect { |
| 230 |
expect(person).to_not have_attributes(:name => correct_name, :parent => 'Billy') |
| 231 |
}.to fail_including("expected #{object_inspect person} to respond to :parent with 0 arguments") |
| 232 |
end |
| 233 |
end |
| 234 |
|
| 235 |
include RSpec::Matchers::Composable |
| 236 |
|
| 237 |
def object_inspect(object) |
| 238 |
surface_descriptions_in object.inspect |
| 239 |
end |
| 240 |
end |