我在 Rails 中有一个功能测试(它是一个 Redmine 插件),这给我带来了问题:
fixtures :settings
test 'error is shown on issues#show when issue custom field is not set up' do
setting = settings(:release_notes)
setting.value = setting.value.
update('issue_required_field_id' => 'garbage')
#setting.save!
get :show, :id => '1'
assert_response :success
assert_select 'div.flash.error',
:text => I18n.t(:failed_find_issue_custom_field)
end
Setting 模型具有字段name
和value
; 在这个特定的设置中,该值是一个被序列化的散列。此散列中的一个键是,用于在显示操作期间issue_required_field_id
查找特定内容。IssueCustomField
如果没有具有此 ID 的自定义字段(不应该存在,因为我已将其设置为字符串 'garbage'),那么它应该呈现一个 div.flash.error 来解释发生了什么。
不幸的是,当setting.save!
被注释掉时,测试失败,因为设置似乎没有更新——使用了该设置的工作值(如 settings.yml 中所示),而“div.flash.error”没有t 出现。如果我取消注释,则此测试通过,但其他测试失败,因为更改未在测试结束时回滚。
有没有办法像这样修改夹具,以便在测试结束时回滚任何更改?
注意:self.use_transactional_fixtures
绝对设置为 true in ActiveSupport::TestCase
(并且这个测试用例是一个ActionController::TestCase
,它是一个子类)