respond_to_missing? breaks apigateway spec
glasswalk3r opened this issue · 1 comments
glasswalk3r commented
The book "Metaprogramming Ruby 2" (method_missing chapter) suggests as a best practice to always declare respond_to_missing
when using method_missing
due possible calls to respond_to?
correctly:
respond_to? calls a method named respond_to_missing? that is supposed to return true if a method is a Ghost Method. (In your mind, you could rename respond_to_missing? to something like ghost_method?.) To prevent respond_to? from lying, override respond_to_missing? every time you override method_missing:
$ git diff
diff --git a/lib/awspec/type/base.rb b/lib/awspec/type/base.rb
index bae7c62..4913702 100644
--- a/lib/awspec/type/base.rb
+++ b/lib/awspec/type/base.rb
@@ -34,6 +34,10 @@ module Awspec::Type
end
end
+ def respond_to_missing?(method, include_private = false)
+ resource_via_client.respond_to?(method) || super unless resource_via_client.nil?
+ end
+
def method_missing(name)
name_str = name.to_s if name.class == Symbol
describe = name_str.tr('-', '_').to_sym
But that breaks the specs from apigateway resource:
$ bundle exec rspec spec/type/apigateway_spec.rb
FFFFFFFFFFFF
Failures:
1) apigateway 'ohx0shePof' should exist
Failure/Error: @id = res.id
NoMethodError:
undefined method `id' for nil:NilClass
# ./lib/awspec/type/apigateway.rb:6:in `cache_values'
# ./lib/awspec/type/apigateway.rb:29:in `id'
# ./lib/awspec/type/resource_base.rb:11:in `exists?'
# ./spec/type/apigateway_spec.rb:5:in `block (2 levels) in <top (required)>'
2) apigateway 'ohx0shePof' id
Failure/Error: @id = res.id
NoMethodError:
undefined method `id' for nil:NilClass
# ./lib/awspec/type/apigateway.rb:6:in `cache_values'
# ./lib/awspec/type/apigateway.rb:29:in `id'
# ./spec/type/apigateway_spec.rb:6:in `block (2 levels) in <top (required)>'
3) apigateway 'ohx0shePof' name
Failure/Error: @id = res.id
NoMethodError:
undefined method `id' for nil:NilClass
# ./lib/awspec/type/apigateway.rb:6:in `cache_values'
# ./lib/awspec/type/apigateway.rb:34:in `name'
# ./spec/type/apigateway_spec.rb:7:in `block (2 levels) in <top (required)>'
4) apigateway 'ohx0shePof' description
Failure/Error: @id = res.id
NoMethodError:
undefined method `id' for nil:NilClass
# ./lib/awspec/type/apigateway.rb:6:in `cache_values'
# ./lib/awspec/type/apigateway.rb:39:in `description'
# ./spec/type/apigateway_spec.rb:8:in `block (2 levels) in <top (required)>'
5) apigateway 'ohx0shePof' created_date
Failure/Error: @id = res.id
NoMethodError:
undefined method `id' for nil:NilClass
# ./lib/awspec/type/apigateway.rb:6:in `cache_values'
# ./lib/awspec/type/apigateway.rb:44:in `created_date'
# ./spec/type/apigateway_spec.rb:9:in `block (2 levels) in <top (required)>'
6) apigateway 'ohx0shePof' version
Failure/Error: @id = res.id
NoMethodError:
undefined method `id' for nil:NilClass
# ./lib/awspec/type/apigateway.rb:6:in `cache_values'
# ./lib/awspec/type/apigateway.rb:49:in `version'
# ./spec/type/apigateway_spec.rb:10:in `block (2 levels) in <top (required)>'
7) apigateway 'ohx0shePof' warnings
Failure/Error: @id = res.id
NoMethodError:
undefined method `id' for nil:NilClass
# ./lib/awspec/type/apigateway.rb:6:in `cache_values'
# ./lib/awspec/type/apigateway.rb:54:in `warnings'
# ./spec/type/apigateway_spec.rb:11:in `block (2 levels) in <top (required)>'
8) apigateway 'ohx0shePof' binary_media_types
Failure/Error: @id = res.id
NoMethodError:
undefined method `id' for nil:NilClass
# ./lib/awspec/type/apigateway.rb:6:in `cache_values'
# ./lib/awspec/type/apigateway.rb:59:in `binary_media_types'
# ./spec/type/apigateway_spec.rb:12:in `block (2 levels) in <top (required)>'
9) apigateway 'ohx0shePof' minimum_compression_size
Failure/Error: @id = res.id
NoMethodError:
undefined method `id' for nil:NilClass
# ./lib/awspec/type/apigateway.rb:6:in `cache_values'
# ./lib/awspec/type/apigateway.rb:64:in `minimum_compression_size'
# ./spec/type/apigateway_spec.rb:13:in `block (2 levels) in <top (required)>'
10) apigateway 'ohx0shePof' api_key_source
Failure/Error: @id = res.id
NoMethodError:
undefined method `id' for nil:NilClass
# ./lib/awspec/type/apigateway.rb:6:in `cache_values'
# ./lib/awspec/type/apigateway.rb:69:in `api_key_source'
# ./spec/type/apigateway_spec.rb:14:in `block (2 levels) in <top (required)>'
11) apigateway 'ohx0shePof' policy
Failure/Error: @id = res.id
NoMethodError:
undefined method `id' for nil:NilClass
# ./lib/awspec/type/apigateway.rb:6:in `cache_values'
# ./lib/awspec/type/apigateway.rb:74:in `policy'
# ./spec/type/apigateway_spec.rb:15:in `block (2 levels) in <top (required)>'
12) apigateway 'my-apigateway' should exist
Failure/Error: @id = res.id
NoMethodError:
undefined method `id' for nil:NilClass
# ./lib/awspec/type/apigateway.rb:6:in `cache_values'
# ./lib/awspec/type/apigateway.rb:29:in `id'
# ./lib/awspec/type/resource_base.rb:11:in `exists?'
# ./spec/type/apigateway_spec.rb:19:in `block (2 levels) in <top (required)>'
Finished in 0.01186 seconds (files took 1.54 seconds to load)
12 examples, 12 failures
Failed examples:
rspec ./spec/type/apigateway_spec.rb:5 # apigateway 'ohx0shePof' should exist
rspec ./spec/type/apigateway_spec.rb:6 # apigateway 'ohx0shePof' id
rspec ./spec/type/apigateway_spec.rb:7 # apigateway 'ohx0shePof' name
rspec ./spec/type/apigateway_spec.rb:8 # apigateway 'ohx0shePof' description
rspec ./spec/type/apigateway_spec.rb:9 # apigateway 'ohx0shePof' created_date
rspec ./spec/type/apigateway_spec.rb:10 # apigateway 'ohx0shePof' version
rspec ./spec/type/apigateway_spec.rb:11 # apigateway 'ohx0shePof' warnings
rspec ./spec/type/apigateway_spec.rb:12 # apigateway 'ohx0shePof' binary_media_types
rspec ./spec/type/apigateway_spec.rb:13 # apigateway 'ohx0shePof' minimum_compression_size
rspec ./spec/type/apigateway_spec.rb:14 # apigateway 'ohx0shePof' api_key_source
rspec ./spec/type/apigateway_spec.rb:15 # apigateway 'ohx0shePof' policy
rspec ./spec/type/apigateway_spec.rb:19 # apigateway 'my-apigateway' should exist
glasswalk3r commented
Fixes provided at PR #438