lib/wcc/contentful/rspec.rb in wcc-contentful-0.4.0.pre.rc vs lib/wcc/contentful/rspec.rb in wcc-contentful-1.0.0.pre.rc1

- old
+ new

@@ -14,21 +14,26 @@ # query methods. def contentful_stub(content_type, **attrs) const = WCC::Contentful::Model.resolve_constant(content_type.to_s) instance = contentful_create(content_type, **attrs) + # mimic what's going on inside model_singleton_methods.rb + # find, find_by, etc always return a new instance from the same raw allow(WCC::Contentful::Model).to receive(:find) - .with(instance.id) - .and_return(instance) - allow(WCC::Contentful::Model).to receive(:find) - .with(instance.id, anything) - .and_return(instance) - allow(const).to receive(:find) { |id, options| WCC::Contentful::Model.find(id, options) } + .with(instance.id, any_args) do |_id, keyword_params| + options = keyword_params && keyword_params[:options] + contentful_create(content_type, options, raw: instance.raw, **attrs) + end + allow(const).to receive(:find) { |id, options| WCC::Contentful::Model.find(id, **(options || {})) } attrs.each do |k, v| allow(const).to receive(:find_by) - .with(hash_including(k => v)) - .and_return(instance) + .with(hash_including(k => v)) do |filter| + filter = filter&.dup + options = filter&.delete(:options) || {} + + contentful_create(content_type, options, raw: instance.raw, **attrs) + end end instance end end