如何测试模型是否具有RSPEC执行期间调用的自定义宏方法
在我的模型中,我在执行过程中运行方法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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
由于这是一种宏观方法,因此最简单的测试方法是:
Since this is a macro method, the easiest way to test this would be:
据我了解,您想检查该
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) }