在我的模型中,我在执行过程中运行方法redefine_associations!:
# frozen_string_literal: true
class MyModule < AnotherModule
redefine_associations!
end
class AnotherModule < ApplicationRecord
def self.redefine_associations!
belongs_to :user
end
redefine_associations!
end如何测试我的模型是否有redefine_associations!。
我尝试了以下几种不起作用的方法:
# frozen_string_literal: true
RSpec.MyModule VisitDate do
it { expect(subject).to have_received(:redefine_associations!) }
it { should respond_to(:redefine_associations!) }
end发布于 2022-06-29 15:23:52
由于这是一个宏方法,测试这一方法的最简单方法是:
let(:class_file_array) do
File.readlines(Rails.root.join('app/models/my_module.rb'))
end
it 'calls redefine_associations!' do
expect(class_file_array.include?(" redefine_associations!\n")).to be true
end发布于 2022-06-29 09:00:03
据我所知,您希望检查为该模型定义的redefine_associations方法。然后你就可以这样做:
it { expect(subject.methods.includes?(:redefine_associations!)).to eql(true) }
https://stackoverflow.com/questions/72794511
复制相似问题