Home > Mobile >  Rails Rspec - How to test if Service has been called in another Service
Rails Rspec - How to test if Service has been called in another Service

Time:04-08

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?

CodePudding user response:

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/

  • Related