【问题标题】:How to update fixture column in Rails Minitest如何更新 Rails Minitest 中的夹具列
【发布时间】:2016-03-18 07:18:44
【问题描述】:

如何仅使用 update_column 命令更新固定装置的列以供临时使用。 现在我有以下运行良好的命令:

name = names(:one)
    role = roles(:one)
    name.role_id = role.id
    assert name.save

它运行良好,但有没有什么有效的方法可以在一行中完成,比如 name.update_column(---, ----) ?

【问题讨论】:

    标签: ruby-on-rails-4 testing minitest fixtures


    【解决方案1】:
    name = names(:one)
    name.update_attributes(role_id: roles(:one).id)
    

    【讨论】:

    • 请添加一些解释。您的答案目前被标记为“低质量”,最终可能会被删除。
    【解决方案2】:

    感谢@richfisher 的回答,稍后我想出了另一种方法。 update_attributes 不是在测试中使用的好主意,因为 update_attributes 的问题是

    • It runs callbacks and validations 通常我们不想在测试用例中运行这些东西

    我们可以像这样使用 update_column 来代替 update_attributes

    name.update_column(:role_id, roles(:one).id)
    

    使用update_column优势

    • It did not run callbacks and validations

    【讨论】:

    • 缺点(有时)还在于它不运行回调或更新。可以使用 update_column 保存无效记录,但在某些情况下会导致测试结果不正确。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-24
    • 2015-10-17
    • 2020-02-23
    相关资源
    最近更新 更多