Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

218
Views
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.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

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
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!