我正在碰壁砖测试类重新定义,只是不知道如何处理它。这是我正在测试的场景(这不是核心数据):
我遇到问题的方法是模拟从内存中实际删除应用程序并从头开始重建它。这很重要,因为在包含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
而不是运行self.included()
的{{1}}方法模块。
关于如何在规范示例的上下文中从头开始模拟此类的创建的任何想法?
答案 0 :(得分:3)
我尝试使用匿名类(您必须告诉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。