【发布时间】:2012-12-31 04:51:28
【问题描述】:
所以,我有点想做类似于 rspec / mocha 的 mock 的事情,但只针对两个对象,而不是全部。这是我目前所拥有的:
def mock(obj, method_to_mock, value)
obj.class << obj do
define_method(method_to_mock) do
return value
end
end
end
我从这篇文章中得到了这样写的想法:https://stackoverflow.com/a/185969/356849
那么我可以做这样的事情:
mock(self.instantiated, :sections, sections)
它会用我的 Section 对象数组sections 覆盖我存储在self.instantiated 的sections 中的对象。
我这样做的原因是因为我正在存储对象的序列化和加密版本,并且我希望能够对对象进行解密和反序列化,然后恢复所有关系,以便我可以在我的视图中查看该对象,就好像它是从数据库中读取的一样。但这并不重要,而且大部分都已经完成了。
所以,我希望能够做到这一点:
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)
目前,我在obj.class << obj do 行遇到错误:
NoMethodError: undefined method `obj' for #<MyObject::Encrypted:0x7f190eebcd18>
想法?
更新
将第二行改为class << obj,现在无限循环。
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'
【问题讨论】:
-
class << self是进入self的单例类的特殊表达式。试试class << obj。class是关键字,不是方法。 -
现在我已经做到了,它无限循环。 o.o 我的猜测是,现在它为
obj的每个实例定义了新方法,而不仅仅是一个。 :-\
标签: ruby-on-rails ruby mocking metaprogramming