加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – Rspec – 存根模块方法

发布时间:2020-12-16 19:37:52 所属栏目:百科 来源:网络整理
导读:如何在模块中存根方法: module SomeModule def method_one # do stuff something = method_two(some_arg) # so more stuff end def method_two(arg) # do stuff endend 我可以隔离测试method_two. 我想通过stubbing method_two的返回值来隔离测试method_one
如何在模块中存根方法:
module SomeModule
    def method_one
        # do stuff
        something = method_two(some_arg)
        # so more stuff
    end

    def method_two(arg)
        # do stuff
    end
end

我可以隔离测试method_two.

我想通过stubbing method_two的返回值来隔离测试method_one:

shared_examples_for SomeModule do
    it 'does something exciting' do
        # neither of the below work
        # SomeModule.should_receive(:method_two).and_return('MANUAL')
        # SomeModule.stub(:method_two).and_return('MANUAL')

        # expect(described_class.new.method_one).to eq(some_value)
    end
end

describe SomeController do
    include_examples SomeModule
end

SomeController中包含的规范失败,因为method_two抛出一个异常(它尝试做一个未被种子的数据库查找).

在method_one中调用的时候如何存根method_two?

解决方法

shared_examples_for SomeModule do
  let(:instance) { described_class.new }

  it 'does something exciting' do
    instance.should_receive(:method_two).and_return('MANUAL')
    expect(instance.method_one).to eq(some_value)
  end
end

describe SomeController do
  include_examples SomeModule
end

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读