首页 > 解决方案 > 为 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

标签: ruby-on-railsrspec

解决方案


skip_callback真的不是为这样的临时使用而设计的。

一个更安全的选择是让 RSpec 存根你的回调方法:

allow_any_instance_of(PropertyPerson).to receive(:generate_match_input_names!).and_return(true)

推荐阅读