Skip to content

Commit

Permalink
Handle nested mock resource attributes better
Browse files Browse the repository at this point in the history
  • Loading branch information
Derek Lindahl committed Mar 26, 2015
1 parent 962a4e4 commit 3391c0c
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
4 changes: 3 additions & 1 deletion lib/frenetic/resource_mockery.rb
@@ -1,6 +1,7 @@
require 'ostruct'
require 'delegate'
require 'active_support/concern'
require 'active_support/core_ext/hash/deep_merge'

class Frenetic
module ResourceMockery
Expand Down Expand Up @@ -44,8 +45,9 @@ def default_attributes
private

def build_params( p )
raw_params = (p || {}).with_indifferent_access
defaults = default_attributes.with_indifferent_access
@params = defaults.merge( (p || {}).with_indifferent_access )
@params = defaults.deep_merge(raw_params)
end

def build_structure
Expand Down
29 changes: 26 additions & 3 deletions spec/resource_mockery_spec.rb
Expand Up @@ -10,18 +10,38 @@
let(:my_mocked_resource) do
Class.new(my_temp_resource) do
def self.default_attributes
{ qux:'qux' }
{
qux: 'qux',
_embedded: {
embedded_resource: {
plugh: 'xyzzy'
}
}
}
end
end
end

let(:params) do
{
foo: 1,
bar: 'baz',
_embedded: {
embedded_resource: {
grault: 'garply'
},
another_resource: {
waldo: 'fred'
}
}
}
end

before do
stub_const 'MyNamespace::MyMockedResource', my_mocked_resource
MyNamespace::MyMockedResource.send :include, described_class
end

let(:params) { { foo:1, bar:'baz' } }

subject { MyNamespace::MyMockedResource.new params }

it 'violates some basic CS principles by telling the parent-class of its existence' do
Expand All @@ -44,6 +64,9 @@ def self.default_attributes
expect(subject).to include 'foo' => 1
expect(subject).to include 'bar' => 'baz'
expect(subject).to include 'qux' => 'qux'
expect(subject['_embedded']['embedded_resource']).to include 'plugh' => 'xyzzy'
expect(subject['_embedded']['embedded_resource']).to include 'grault' => 'garply'
expect(subject['_embedded']['another_resource']).to include 'waldo' => 'fred'
end
end

Expand Down

0 comments on commit 3391c0c

Please sign in to comment.