Debian Codegraph @

rspec-expectations/spec/rspec/matchers/built_in/be_instance_of_spec.rb

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