'Stub a method inside other method
module A
module B
class C
def fun(github)
return username
end
def gun(github)
author=fun(github)
#perfom some operation
return count
end
end
end
I want to test gun function, but to get the value of author, it is making a call to function fun. Now, I do not want to call this function and instead stub it.
To achieve that, I wrote this line in the before do section while testing the function_1, But it's not working.
allow(A::B::C).to receive(:fun).and_return("username")
I cannot figure out.
Solution 1:[1]
Both methods are instance methods, not class methods, therefore you need to stub the method on the instance.
The following might work for you:
let(:abc) { A:B.C.new }
let(:github) { ... }
before { allow(abc).to receive(:fun).and_return("username") }
it "..." do
expect(abc.fun(github)).to eq(...)
expect(abc).to have_received(:fun).with(github)
end
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
