gpt4 book ai didi

ruby-on-rails - 如何使用 rspec 测试模型上没有修改任何属性?

转载 作者:行者123 更新时间:2023-12-04 22:06:57 25 4
gpt4 key购买 nike

我想检查是否没有修改 ActiveRecord 对象上的属性。目前我正在这样做:
prev_attr = obj.attributes <- 这将返回一个带有 attr 名称和 attr 值的哈希

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

最佳答案

确实有另一种方式。你可以这样做:

it "should not change sth" do
expect {
# some action
}.to_not change{subject.attribute}
end

https://www.relishapp.com/rspec/rspec-expectations/v/2-0/docs/matchers/expect-change .

关于ruby-on-rails - 如何使用 rspec 测试模型上没有修改任何属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10478746/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com