ruby 运动 : Removing a Ruby Class Completely from Object Space

标签 ruby testing rubymotion

我遇到了砖墙测试类重新定义的问题,只是不知道如何处理它。这是我正在测试的场景(这不是核心数据):

  • 应用程序使用版本 1 中的模型运行
  • 渴望程序员通过添加/删除/重新定义列来修改模型
  • 应用程序使用版本 2 中的模型运行

我遇到问题的地方是模拟从内存中实际删除应用程序并从头开始重建它。这很重要,因为在包含 MotionModel::Model 模块时会设置许多特定于模型的东西,并且只会发生一次:当模块包含在类中时。以下是我认为可能有效的方法:

  it "column removal" do
      class Removeable
        include MotionModel::Model
        columns       :name => :string, :desc => :string
      end


      @foo = Removeable.create(:name=> 'Bob', :desc => 'who cares anyway?')


      Removeable.serialize_to_file('test.dat')

      @foo.should.respond_to :desc

      Object.send(:remove_const, :Removeable)  # Should remove all traces of Removeable
      class Removeable
        include MotionModel::model             # Should include this again instead
        columns       :name => :string,        # of just reopening the old Removeable
                      :address => :string      # class
      end


      Removeable.deserialize_from_file # Deserialize old data into new model


      Removeable.length.should == 1
      @bar = Removeable.first
      @bar.should.respond_to :name
      @bar.should.respond_to :address      
      @bar.should.not.respond_to :desc


      @bar.name.should == 'Bob'
      @bar.address.should == nil
    end
  end

不幸的是,Object.send(:remove_const, :Removeable) 没有达到我希望的效果,Ruby 只是认为它可以重新打开 Removeable 而不是运行 MotionModel::Model 模块的 self.included() 方法。

关于如何在规范示例的上下文中从头开始模拟创建此类的任何想法?

最佳答案

我会尝试使用匿名类(您必须将表名告诉 MotionModel)。

虚构的例子:

model_before_update = Class.new do
  # This tells MotionModel the name of the class (don't know if that actually exists)
  table_name "SomeTable"
  include MotionModel::Model
  columns       :name => :string, :desc => :string
end

您根本没有删除该类,您只是定义了另一个具有相同表名的(匿名)类。

model_after_update = Class.new do
  table_name "SomeTable"
  include MotionModel::model
  columns       :name => :string,
                :address => :string
end

考虑到这一点,如果有一个像上面那样的 table_name setter,你甚至不需要使用匿名类,以防 RubyMotion 不工作。

关于 ruby 运动 : Removing a Ruby Class Completely from Object Space,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13294542/

相关文章:

ruby-on-rails - rails i18n 日期问题

ruby-on-rails - Rails 在 Controller 中使用哈希

macos - RubyMotion 中的指针与 MacRuby 中的指针

ios - Ruby Motion 如何更改后退按钮的默认颜色?

objective-c - RubyMotion 是否支持 objective-c 代码的集成?

ruby-on-rails - 迁移时出现以下错误

ruby - 如何使用服务器上的本地资源?

macos - Mac 上的 IE7-9 测试工作流程

Android MVP - 应该避免在演示者中使用 R.string 引用吗?

c# - TDD : Any pattern for constant testing?