1

所以,我有点想做类似于 rspec / mocha's 的事情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 对象数组覆盖我存储在self.instantiated's中的对象。sectionssections

我这样做的原因是因为我正在存储对象的序列化和加密版本,并且我希望能够对对象进行解密和反序列化,然后恢复所有关系以便我可以查看在我的视图中的对象,就好像它是从数据库中读取的一样。但这并不重要,而且大部分已经完成。

所以,我希望能够做到这一点:

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'
4

2 回答 2

2
def mock(obj, method_to_mock, value=nil)
  obj.define_singleton_method(method_to_mock) do value end
end  
于 2013-01-16T19:49:45.607 回答
1

obj.class << obj do没有意义。

你可能想说的是

def mock(obj, method_to_mock, value)
  (class << obj; self; end).class_eval do
    define_method(method_to_mock) do
      return value
    end
  end
end

(class << obj; self; end).class_eval语法是打开返回该单例类的单obj例类,然后在传递该块的单例类上调用 class_eval。

在您的语法中,obj.class:class消息obj作为接收器发送到接收器,该接收器返回对 obj 的类(而不是其单例类)的引用,然后您尝试在该接收器上调用将<<评估结果obj do...end作为 arg 传递的方法。由于obj不是self(MyObject::Encrypted:0x7f190eebcd1) 的方法,您会得到 NoMethodError。

在现代 ruby​​ 中,不要说相对神秘,(class << obj; self; end)要获得单例类,您可以使用如下singleton_class方法:obj.singleton_class.class_eval do ... end

于 2013-01-16T19:24:24.023 回答