我有下面的测试,如果这个模型有任何固定装置,它会失败,total_unapproved 和 new_total 相等,而不是 new_total 少一个。如果我删除加载 test_helper.rb 中的固定装置或将它们注释掉,它会按我的预期运行。
这是将批准设置为 true 的类函数。它绝对有效。
def InviteRequest.approve_invites(number)
inv_reqs = InviteRequest.where("approved = ?", false).first(number)
inv_reqs.each do |inv_req|
inv_req.approved = true
inv_req.save
inv_req.send_approved_email
end
end
这是调用上述函数的测试。
require 'test_helper'
class InviteRequestTest < ActiveSupport::TestCase
test "class method approve_invites(number) should approve 'number' InviteRequests" do
# ensure there is at least one instance
inv_req = InviteRequest.create(email: "hobojoe@test.com")
# set all InviteRequests.approved to false
InviteRequest.all.each {|r| r.approved = false; r.save}
total_unapproved = InviteRequest.where("approved = ?", false).count
Rails.logger.info "\nUnapproved Before: #{total_unapproved}"
InviteRequest.approve_invites(1)
new_total = InviteRequest.where("approved = ?", false).count
Rails.logger.info "Unapproved After: #{new_total}\n"
assert_equal total_unapproved - 1, new_total
end
end
知道为什么吗?我没有在任何其他测试中使用固定装置,但也许有一天我会。