首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何测试模型是否具有在Rspec中执行过程中调用的自定义宏方法

如何测试模型是否具有在Rspec中执行过程中调用的自定义宏方法
EN

Stack Overflow用户
提问于 2022-06-29 00:28:02
回答 2查看 70关注 0票数 1

在我的模型中,我在执行过程中运行方法redefine_associations!

代码语言:javascript
复制
# 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!

我尝试了以下几种不起作用的方法:

代码语言:javascript
复制
# frozen_string_literal: true
RSpec.MyModule VisitDate do
  it { expect(subject).to have_received(:redefine_associations!) }
  it { should respond_to(:redefine_associations!) }
end
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2022-06-29 15:23:52

由于这是一个宏方法,测试这一方法的最简单方法是:

代码语言:javascript
复制
  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
票数 1
EN

Stack Overflow用户

发布于 2022-06-29 09:00:03

据我所知,您希望检查为该模型定义的redefine_associations方法。然后你就可以这样做:

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72794511

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档