ruby-on-rails - 为 rspec 测试禁用回调后,如何重新启用回调?
问题描述
我有以下代码,但我的其他测试方法失败,因为 :before_save 回调没有在单独的测试中被触发。
before do
PropertyPerson.skip_callback :save, :before, :generate_match_input_names!, raise: false
end
describe :with_search_name_fuzzy do
it 'finds the property_person' do
property_person = property.property_people.create(person: person, match_input_search_names: ['Kamil Makski'])
expect(PropertyPerson.with_search_name_fuzzy('KAM')).to be_present
end
end
解决方案
skip_callback
真的不是为这样的临时使用而设计的。
一个更安全的选择是让 RSpec 存根你的回调方法:
allow_any_instance_of(PropertyPerson).to receive(:generate_match_input_names!).and_return(true)
推荐阅读
- c# - 在 WebAPI 中返回一对多列表应该放什么?
- apache-drill - 如何在 Apache Drill API 响应中禁用科学记数法
- laravel - Retrieve the owner of a polymorphic relationship laravel
- javascript - 基于背景颜色的倒置标志
- ruby-on-rails - 未选中时,Recaptcha Gem 不会阻止提交(Rails 5.2)
- c++ - c ++:清除向量的向量似乎将向量元素留在原处
- javascript - 从另一个 json 数组项中查找数组项的索引
- swiftui - 如何将 EnvironmentObject 变量绑定到滑块
- bash - 如果第 2 列相同,则减去第 1 列中的值
- python - 如何使用 Google 服务对 Docker 容器进行身份验证