【发布时间】: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