So, I'm kind of wanting to do something similar to rspec / mocha's mock
, but only for two objects, and not all of them. This is what I have so far:
def mock(obj, method_to_mock, value)
obj.class << obj do
define_method(method_to_mock) do
return value
end
end
end
I got the idea to write it like that from this post: https://stackoverflow.com/a/185969/356849
So then I can do things like:
mock(self.instantiated, :sections, sections)
and it would override the object I have stored in self.instantiated
's sections
with my array of Section objects, sections
.
The reason why I'm doing this, is because I'm storing a serialized and encrypted version of of an object, and I want to be able to unencrypt and unserialize the object, and then restore all the relationships such that I can view that object in my views, as if it were being read from the database. but that's not important, and most of it's done.
So, I want to be able to do this:
mock(<Instance of object>, :<method of object that is going to be overridden, to avoid db access>, <the stuff to return when the overridden method is invoked)
CUrrently, I'm getting an error on the obj.class << obj do
line with this:
NoMethodError: undefined method `obj' for #<MyObject::Encrypted:0x7f190eebcd18>
ideas?
UPDATE
changed the second line to class << obj
which now infinite loops.
from /home/me/.rvm/gems/ruby-1.8.7-p371@project/gems/activerecord-2.3.15/lib/active_record/connection_adapters/abstract/connection_pool.rb:351:in `retrieve_connection_pool'
from /home/me/.rvm/gems/ruby-1.8.7-p371@project/gems/activerecord-2.3.15/lib/active_record/connection_adapters/abstract/connection_pool.rb:351:in `retrieve_connection_pool'
from /home/me/.rvm/gems/ruby-1.8.7-p371@project/gems/activerecord-2.3.15/lib/active_record/connection_adapters/abstract/connection_pool.rb:325:in `retrieve_connection'
from /home/me/.rvm/gems/ruby-1.8.7-p371@project/gems/activerecord-2.3.15/lib/active_record/connection_adapters/abstract/connection_specification.rb:123:in `retrieve_connection'
from /home/me/.rvm/gems/ruby-1.8.7-p371@project/gems/activerecord-2.3.15/lib/active_record/connection_adapters/abstract/connection_specification.rb:115:in `connection'
from /home/me/.rvm/gems/ruby-1.8.7-p371@project/gems/activerecord-2.3.15/lib/active_record/base.rb:1305:in `columns'
from /home/me/.rvm/gems/ruby-1.8.7-p371@project/gems/activerecord-2.3.15/lib/active_record/base.rb:1318:in `column_names'
from /home/me/.rvm/gems/ruby-1.8.7-p371@project/gems/searchlogic-2.4.28/lib/searchlogic/named_scopes/ordering.rb:35:in `ordering_condition_details'
from /home/me/.rvm/gems/ruby-1.8.7-p371@project/gems/searchlogic-2.4.28/lib/searchlogic/named_scopes/ordering.rb:26:in `method_missing'
from /home/me/.rvm/gems/ruby-1.8.7-p371@project/gems/searchlogic-2.4.28/lib/searchlogic/named_scopes/or_conditions.rb:28:in `method_missing'
from /home/me/.rvm/gems/ruby-1.8.7-p371@project/gems/activerecord-2.3.15/lib/active_record/base.rb:2002:in `method_missing_without_paginate'
from /home/me/.rvm/gems/ruby-1.8.7-p371@project/gems/will_paginate-2.3.16/lib/will_paginate/finder.rb:170:in `method_missing_without_attr_encrypted'
from /home/me/.rvm/gems/ruby-1.8.7-p371@project/bundler/gems/attr_encrypted-a4b25f01d137/lib/attr_encrypted/adapters/active_record.rb:50:in `method_missing'
from /home/me/Work/GravityLabs/project/app/models/proposal/encrypted.rb:119:in `mock'
from /home/me/Work/GravityLabs/project/app/models/proposal/encrypted.rb:79:in `instantiate'
from /home/me/Work/GravityLabs/project/app/models/proposal/encrypted.rb:58:in `each'
from /home/me/Work/GravityLabs/project/app/models/proposal/encrypted.rb:58:in `instantiate'