如何使用 rspec 测试模型上没有修改任何属性? [英] How can I test that no attribute on a model has been modified using rspec?

查看:37
本文介绍了如何使用 rspec 测试模型上没有修改任何属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想检查是否没有修改 ActiveRecord 对象上的属性.目前我正在这样做:

I want to check if no attributes on an ActiveRecord object have been modified. Currently I'm doing this:

prev_attr = obj.attributes <- 这将返回一个带有属性名称和属性值的哈希

prev_attr = obj.attributes <- this will give me back a Hash with attr name and attr value

然后,稍后,我再次获取属性并比较 2 个哈希值.还有别的方法吗?

And then, later, I grab the attributes again and compare the 2 hashes. Is there another way?

推荐答案

你应该能够使用 平等匹配器 - 这对您不起作用吗?

You should just be able to use equality matchers - does this not work for you?

a = { :test => "a" }
b = { :test => "b" }
$ a == b
=> false
b = { :test => "a" }
$ a == b
=> true

或者用你的例子:

original_attributes = obj.attributes
# do something that should *not* manipulate obj
new_attributes = obj.attributes
new_attributes.should eql original_attributes

这篇关于如何使用 rspec 测试模型上没有修改任何属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆