| 1 | module RSpec |
| 2 | module Matchers |
| 3 | [:be_a_kind_of, :be_kind_of].each do |method| |
| 4 | RSpec.describe "expect(actual).to #{method}(expected)" do |
| 5 | it_behaves_like "an RSpec value matcher", :valid_value => 5, :invalid_value => "a" do |
| 6 | let(:matcher) { send(method, Integer) } |
| 7 | end |
| 8 | |
| 9 | it "passes if actual is instance of expected class" do |
| 10 | expect("string").to send(method, String) |
| 11 | end |
| 12 | |
| 13 | it "passes if actual is instance of subclass of expected class" do |
| 14 | expect(5).to send(method, Numeric) |
| 15 | end |
| 16 | |
| 17 | it "fails with failure message for should unless actual is kind of expected class" do |
| 18 | expect { |
| 19 | expect("foo").to send(method, Array) |
| 20 | }.to fail_with('expected "foo" to be a kind of Array') |
| 21 | end |
| 22 | |
| 23 | it "provides a description" do |
| 24 | matcher = be_a_kind_of(String) |
| 25 | matcher.matches?("this") |
| 26 | expect(matcher.description).to eq "be a kind of String" |
| 27 | end |
| 28 | |
| 29 | context "when the actual object does not respond to #kind_of? method" do |
| 30 | let(:actual_object) do |
| 31 | Class.new { undef_method :kind_of? }.new |
| 32 | end |
| 33 | |
| 34 | it "raises ArgumentError" do |
| 35 | message = "The be_a_kind_of matcher requires that " \ |
| 36 | "the actual object responds to #kind_of? method " \ |
| 37 | "but a `NoMethodError` was encountered instead." |
| 38 | |
| 39 | expect { |
| 40 | expect(actual_object).to send(method, actual_object.class) |
| 41 | }.to raise_error ::ArgumentError, message |
| 42 | |
| 43 | expect { |
| 44 | expect(actual_object).to send(method, Object) |
| 45 | }.to raise_error ::ArgumentError, message |
| 46 | end |
| 47 | end |
| 48 | |
| 49 | context "when the actual object does not respond to #is_a? method" do |
| 50 | let(:actual_object) do |
| 51 | Class.new { undef_method :is_a? }.new |
| 52 | end |
| 53 | |
| 54 | it "provides correct result" do |
| 55 | expect(actual_object).to send(method, actual_object.class) |
| 56 | expect(actual_object).to send(method, Object) |
| 57 | end |
| 58 | end |
| 59 | end |
| 60 | |
| 61 | RSpec.describe "expect(actual).not_to #{method}(expected)" do |
| 62 | it "fails with failure message for should_not if actual is kind of expected class" do |
| 63 | expect { |
| 64 | expect("foo").not_to send(method, String) |
| 65 | }.to fail_with('expected "foo" not to be a kind of String') |
| 66 | end |
| 67 | |
| 68 | context "when the actual object does not respond to #kind_of? method" do |
| 69 | let(:actual_object) do |
| 70 | Class.new { undef_method :kind_of? }.new |
| 71 | end |
| 72 | |
| 73 | it "raises ArgumentError" do |
| 74 | message = "The be_a_kind_of matcher requires that " \ |
| 75 | "the actual object responds to #kind_of? method " \ |
| 76 | "but a `NoMethodError` was encountered instead." |
| 77 | |
| 78 | expect { |
| 79 | expect(actual_object).not_to send(method, String) |
| 80 | }.to raise_error ArgumentError, message |
| 81 | end |
| 82 | end |
| 83 | |
| 84 | context "when the actual object does not respond to #is_a? method" do |
| 85 | let(:actual_object) do |
| 86 | Class.new { undef_method :is_a? }.new |
| 87 | end |
| 88 | |
| 89 | it "provides correct result" do |
| 90 | expect(actual_object).not_to send(method, String) |
| 91 | end |
| 92 | end |
| 93 | end |
| 94 | end |
| 95 | end |
| 96 | end |