如何解决如何模拟关注中的方法?
我有 FooConcern
和 BarService
如下:
module FooConcern
extend ActiveSupport::Concern
def notify_slack(message)
# send message to slack
end
end
class BarService
include FooConcern
def run
# do some stuff
message = 'blah,blah'
notify_slack(message)
end
end
如何编写模拟 notify_slack
以便在运行 BarService#run
的 Rspec 测试时实际上不会调用 slack API?
RSpec.describe BarService do
describe 'run' do
subject { described_class.new.run }
it do
# some tests on things other than notifying slack
end
end
end
解决方法
您可以使用 allow 来模拟该方法。
RSpec.describe BarService do
describe 'run' do
subject { described_class.new }
it do
allow(subject).to receive(:notify_slack).and_return(true)
# some tests on things other than notifying slack
subject.run
end
end
end
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。