我遇到了一个砖墙测试类的重新定义,只是不知道如何处理它。下面是我正在测试的场景(这不是核心数据):
我遇到的问题是模拟从内存中实际删除应用程序并从头开始重建应用程序。这一点很重要,因为当包含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()方法。
对于如何在规范示例的上下文中从无到有地模拟创建该类,有什么想法吗?
发布于 2012-12-03 16:30:25
我尝试使用匿名类(您必须告诉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设置器,那么您甚至不需要使用匿名类,以防RubyMotion无法工作。
https://stackoverflow.com/questions/13294542
复制相似问题