【问题标题】:Rspec test if an attribute was updatedRspec 测试属性是否已更新
【发布时间】:2011-06-22 10:09:25
【问题描述】:

我的模型有一个方法可以从远程资源更新模型中的多个属性。我想用 Rspec 进行测试,但找不到如何测试字段是否已创建或更新。

一些伪代码来解释这个问题

def update_from_remote
  attributes = {}
  fields.each_key do |field_name|
    attributes[field_name] = scrape_from_remote field_name
  end
  update_attributes(attributes)
end

实际上,这段代码要复杂得多,但这只会使我的问题变得混乱。 create_from_remote 非常相似,只是它不调用 update_attributes 而是简单地设置它们然后保存对象。

我想测试一下。我想要的是一个测试字段是否已更新或填充的规范:

it 'should fill or set all local attributes on a new profile' do
  #how to test if a list of fields were updated in the Database?
end
it 'should update all local attributes on an existing profile' do
  #how to test if a list of fields were created in the Database?
end

我使用的是 mongoId,但 AFAIK 应该没什么区别。

【问题讨论】:

    标签: ruby-on-rails-3 unit-testing rspec mongoid


    【解决方案1】:

    我应该深入研究 MongoID 文档。 Dirty tracking上有一个章节可以使用:

    it "should be updated"
      @something.field.should be_changed
    end
    

    【讨论】:

      猜你喜欢
      • 2019-05-29
      • 1970-01-01
      • 2013-12-28
      • 2015-02-20
      • 2016-06-24
      • 1970-01-01
      • 1970-01-01
      • 2019-04-04
      • 1970-01-01
      相关资源
      最近更新 更多