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

531
Views
Rails Rspec - How to test if Service has been called in another Service

While writing tests, I stopped at trying to test Service in another Service. In such a situation, I should probably just check if Service has been called because it has already been tested elsewhere. I did a little research on the Internet and found something like have_received but I have no idea how to use it in my example.

check_service.rb

Class CheckService
 def initialize(params)
   @params = params
 end

 def self.call(params)
   new(params).call
 end

 def call
   CheckUser.call(params[:user_id])
 end
end

check_service_spec.rb

...

describe 'call' do
 let(:result) { CheckService.call(params) }
 let(:params) { { user_id: "100" } }
 let(:check_user) { instance_double(CheckUser) }

 before do
   allow(check_user).to receive(:call).and_return(true)
 end

 it do 
  result
  expect(check_user).to have_received(:call) 
 end
end
...

I was trying something like this (it's simple example), but I get error:

(InstanceDouble(CheckUser) (anonymous)).call(*(any args))
           expected: 1 time with any arguments
           received: 0 times with any arguments

Is there any option to test situation I presented?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Short anwser

describe 'call' do
 let(:result) { CheckService.call(params) }
 let(:params) { { user_id: "100" } }
 ## let(:check_user) { instance_double(CheckUser) } delete this

 before do
   allow(CheckUser).to receive(:call).and_return(true)
 end

 it do 
  result
  expect(CheckUser).to have_received(:call) 
 end
end

Alternative

I think a better way to test this is to use DI (Dependency Injection), so you pass CheckUser as a dependency to CheckService. I prefer to write the whole test inside the it block too!

class CheckService
  def initialize(params, check_handler:)
    @params = params
    @check_handler = check_handler
  end

  def self.call(params, check_handler: CheckUser)
    new(params, check_handler: check_handler).call
  end

  def call
    @check_handler.call(@params[:user_id])
  end
end
describe 'call' do
  it 'check user with params' do
    check_user = class_double(CheckUser)
    allow(check_user).to receive(:call).and_return(true)

    params = { user_id: "100" }
    CheckService.call(params, check_handler: check_user)

    expect(check_user).to have_received(:call)
  end
end

A blog post to read more about -> https://blog.testdouble.com/posts/2018-05-17-do-we-need-dependency-injection-in-ruby/

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!