如何测试模型是否具有RSPEC执行期间调用的自定义宏方法

发布于 2025-02-11 22:18:19 字数 597 浏览 2 评论 0原文

在我的模型中,我在执行过程中运行方法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

In my model, I run the method redefine_associations! during execution:

# frozen_string_literal: true
class MyModule < AnotherModule
  redefine_associations!
end

class AnotherModule < ApplicationRecord
  def self.redefine_associations!
    belongs_to :user
  end

  redefine_associations!
end

How can I test that my model has redefine_associations!.

I tried the following which did not work:

# frozen_string_literal: true
RSpec.MyModule VisitDate do
  it { expect(subject).to have_received(:redefine_associations!) }
  it { should respond_to(:redefine_associations!) }
end

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

怀念你的温柔 2025-02-18 22:18:19

由于这是一种宏观方法,因此最简单的测试方法是:

  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

Since this is a macro method, the easiest way to test this would be:

  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
作妖 2025-02-18 22:18:19

据我了解,您想检查该redefine_associations是否为该模型定义的方法。然后,您可以这样做:

it {priect.methods.includes?(:redefine_associations!))。到eql(true)}>

As i understand you want to check if that redefine_associations method defined for that model. Then you can do it like this:

it { expect(subject.methods.includes?(:redefine_associations!)).to eql(true) }

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文