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
|
require 'support/doubled_classes'
module RSpec
module Mocks
RSpec::Matchers.define :fail_expectations_as do |expected|
description { "include a meaningful name in the exception" }
def error_message_for(_)
expect(actual).to have_received(:defined_instance_and_class_method)
rescue MockExpectationError, Expectations::ExpectationNotMetError => e
e.message
else
raise("should have failed but did not")
end
failure_message do |actual|
"expected #{actual.inspect} to fail expectations as:\n" \
" #{expected.inspect}, but failed with:\n" \
" #{@error_message.inspect}"
end
match do |actual|
@error_message = error_message_for(actual)
@error_message.include?(expected)
end
end
RSpec.describe 'Verified double naming' do
shared_examples "a named verifying double" do |type_desc|
context "when a name is given as a string" do
subject { create_double("LoadedClass", "foo") }
it { is_expected.to fail_expectations_as(%Q{#{type_desc}(LoadedClass) "foo"}) }
end
context "when a name is given as a symbol" do
subject { create_double("LoadedClass", :foo) }
it { is_expected.to fail_expectations_as(%Q{#{type_desc}(LoadedClass) :foo}) }
end
context "when no name is given" do
subject { create_double("LoadedClass") }
it { is_expected.to fail_expectations_as(%Q{#{type_desc}(LoadedClass) (anonymous)}) }
end
end
describe "instance_double" do
it_behaves_like "a named verifying double", "InstanceDouble" do
alias :create_double :instance_double
end
end
describe "instance_spy" do
it_behaves_like "a named verifying double", "InstanceDouble" do
alias :create_double :instance_spy
end
end
describe "class_double" do
it_behaves_like "a named verifying double", "ClassDouble" do
alias :create_double :class_double
end
end
describe "class_spy" do
it_behaves_like "a named verifying double", "ClassDouble" do
alias :create_double :class_spy
end
end
describe "object_double" do
it_behaves_like "a named verifying double", "ObjectDouble" do
alias :create_double :object_double
end
end
describe "object_spy" do
it_behaves_like "a named verifying double", "ObjectDouble" do
alias :create_double :object_spy
end
end
end
end
end
|