2011-06-22 74 views
3

我的模型有一个方法可以从远程资源更新模型中的几个属性。我想用Rspec来测试,但是无法找到如何测试字段是否被创建或更新。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 

在现实中,这个代码是一个复杂得多,但只会弄乱我的问题。 A 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,但据我所知,不应该太大的差别。

回答

4

我应该深入挖掘MongoID文档。有关于Dirty tracking的章节可以使用:

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