小编Ang*_*ian的帖子

Rspec如何存根模块实例方法?

B.rb看起来像:

module A
   module B

     def enabled? xxx
        xxx == 'a'
     end

     def disabled? xxx
       xxx != 'a'
     end
   end
end
Run Code Online (Sandbox Code Playgroud)

另一个C.rb如:

module YYY
  class C
  include A::B

  def a_method 
     if(enabled? xxx) 
       return 'a'
     return 'b'
  end
end
Run Code Online (Sandbox Code Playgroud)

现在我想编写单元测试来测试a_method函数,

 describe :a_method do
    it 'returns a' do
       ###how to sub enabled? method to make it return true....
    end
 end 
Run Code Online (Sandbox Code Playgroud)

启用?是模型中的实例方法,我试过了

A::B.stub.any_instance(:enabled).and_return true
Run Code Online (Sandbox Code Playgroud)

它不起作用。

有人可以帮助我吗?

rspec ruby-on-rails

5
推荐指数
1
解决办法
4714
查看次数

标签 统计

rspec ×1

ruby-on-rails ×1