From 7cc91f9b4af9066c47bba8d08992be70adac8328 Mon Sep 17 00:00:00 2001 From: Elliot Winkler Date: Mon, 24 Jul 2017 23:31:56 -0500 Subject: [PATCH] Doublespeak: Define #respond_to_missing? on ObjectDouble The `delegate_method` matcher uses Doublespeak internally to do its job. The delegate object is completely stubbed using an ObjectDouble, available from Doublespeak, which is shoulda-matchers's own test double library. ObjectDouble is a class that responds to every method by implementing `method_missing`. Say you are using Ruby 2.4 and you are testing a class that uses the Forwardable module to do some delegation, and you are using `delegate_method` in your test. When you run your test you will a warning that looks something like: Courier#deliver at ~/.rvm/rubies/ruby-2.4.0/lib/ruby/2.4.0/forwardable.rb:156 forwarding to private method #deliver Why is this happening? When the code in your class gets exercised, Forwardable will delegate the delegate method in question to ObjectDouble, and the method will get intercepted by its `method_missing` method. The fact that this is actually a private method is what Forwardable is complaining about. To fix this, all we need to do is add `respond_to_missing?` in addition to `method_missing?`. This is explained here: https://bugs.ruby-lang.org/issues/13326 --- lib/shoulda/matchers/doublespeak/object_double.rb | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/shoulda/matchers/doublespeak/object_double.rb b/lib/shoulda/matchers/doublespeak/object_double.rb index 823797b3..17b27af4 100644 --- a/lib/shoulda/matchers/doublespeak/object_double.rb +++ b/lib/shoulda/matchers/doublespeak/object_double.rb @@ -14,7 +14,11 @@ module Shoulda @calls_by_method_name[method_name] || [] end - def respond_to?(name, include_private = nil) + def respond_to?(_name, _include_private = nil) + true + end + + def respond_to_missing?(_name, _include_all) true end