测试一条记录是否与 RSpec 中的另一条记录重复
Test that a record is a duplicate of another record in RSpec
在我使用 RSpec 测试的 Rails 应用程序中,我正在测试 calls .dup()
在某些记录上并保存重复项的功能。如何测试新创建的记录是否与预期的原始记录重复?
我怀疑是否存在针对此特定案例的断言。而且我始终可以确保重复记录和原始记录的属性值相同,但却是不同的记录。我想知道这种测试是否有更优雅、更可接受的模式。
您可以使用 have_attributes
断言。
match_attributes = first_user.attributes.except(:id, :created_at, :updated_at)
expect(second_user).to have_attributes(match_attributes)
你可以这样做:
it 'creates a duplicate' do
record = Record.find(1)
new_record = record.dup
new_record.save
expect(Record.where(id: [record.id,new_record.id],
**new_record.attributes.except(:id, :created_at, :updated_at)
).count
).to eq 2
end
这应该量化一次记录的重复和持久性。
在我使用 RSpec 测试的 Rails 应用程序中,我正在测试 calls .dup()
在某些记录上并保存重复项的功能。如何测试新创建的记录是否与预期的原始记录重复?
我怀疑是否存在针对此特定案例的断言。而且我始终可以确保重复记录和原始记录的属性值相同,但却是不同的记录。我想知道这种测试是否有更优雅、更可接受的模式。
您可以使用 have_attributes
断言。
match_attributes = first_user.attributes.except(:id, :created_at, :updated_at)
expect(second_user).to have_attributes(match_attributes)
你可以这样做:
it 'creates a duplicate' do
record = Record.find(1)
new_record = record.dup
new_record.save
expect(Record.where(id: [record.id,new_record.id],
**new_record.attributes.except(:id, :created_at, :updated_at)
).count
).to eq 2
end
这应该量化一次记录的重复和持久性。