首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Rspec - stub模块方法

Rspec - stub模块方法
EN

Stack Overflow用户
提问于 2013-08-20 19:26:33
回答 2查看 10K关注 0票数 16

如何在模块中存根方法:

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

我也想通过存根method_two的返回值来单独测试method_one

代码语言:javascript
复制
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中包含的SomeModule规范会失败,因为method_two会抛出异常(它会尝试执行尚未设定种子的数据库查找)。

method_one中调用method_two时,如何对其进行存根

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-09-21 02:22:03

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

Stack Overflow用户

发布于 2015-06-11 20:31:52

代码语言:javascript
复制
allow_any_instance_of(M).to receive(:foo).and_return(:bar)

Is there a way to stub a method of an included module with Rspec?

这种方法对我很有效。

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

https://stackoverflow.com/questions/18333821

复制
相关文章

相似问题

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