【问题标题】:Test that a record is a duplicate of another record in RSpec测试一条记录是否与 RSpec 中的另一条记录重复
【发布时间】:2019-05-17 16:54:14
【问题描述】:

在使用 RSpec 测试的 Rails 应用程序中,我正在测试 calls .dup() 在某些记录上并保存重复项的函数。如何测试新创建的记录是否与预期的原始记录重复?

我怀疑是否有针对此特定案例的断言。而且我始终可以确保重复记录和原始记录的属性值相同,但它们是不同的记录。我想知道这种测试是否有更优雅、更可接受的模式。

【问题讨论】:

    标签: ruby-on-rails rspec rspec-rails


    【解决方案1】:

    您可以使用have_attributes 断言。

    match_attributes = first_user.attributes.except(:id, :created_at, :updated_at)
    
    expect(second_user).to have_attributes(match_attributes)
    

    【讨论】:

      【解决方案2】:

      你可以这样做:

       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
      

      这应该一次性量化两个记录的重复和持久性。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-02-02
        • 2018-09-01
        • 2017-05-08
        • 2019-09-30
        • 1970-01-01
        • 2018-08-28
        相关资源
        最近更新 更多