如何在 Rails Minitest 中更新夹具列

How to update fixture column in Rails Minitest

如何使用 update_column 命令更新夹具的列以供临时使用。 现在我有以下命令 运行 很好:

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

运行 很好,但是有没有什么有效的方法可以像 name.update_column(---, ----) 这样在一行中做到这一点?

name = names(:one)
name.update_attributes(role_id: roles(:one).id)

感谢@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