Can somebody help me with rspec testing method call in Service Object?
class UserEntitiesController < ApplicationController
def create
@result = UserEntities::Create.new(params).call
return render '/422.json.jbuilder', status: :unprocessable_entity unless @result
end
here is the service objects:
module UserEntities
class Create
attr_accessor :params
def initialize(params)
@params = params
end
def call
@user_entity = UserEntity.new(user_entity_params)
set_time
if @user_entity.save
@user_entity
else
error_result
end
end
private
def error_result
false
end
def user_entity_params
@params.require(:user_entity).permit(:information,
:destroy_option,
:reviews)
end
def set_time
if @params[:available_days].present?
@user_entity.termination = Time.now + @params[:available_days].days
end
end
end
end
I tried to find information how to do this, but there are not so many. Also i read some
You can certainly write a unit test to test the Service Object
standalone
In this case, create a file spec/services/user_entities/create_spec.rb
describe UserEntities::Create do
let(:params) { #values go here }
context ".call" do
it "create users" do
UserEntities::Create.new(params).call
# more test code
end
# more tests
end
end
Later in the controller tests, if you are planning to write such, you do not need to test UserEntities::Create
instead you can just mock the service object to return the desired result
describe UserEntitiesController do
before do
# to mock service object in controller test
allow(UserEntities::Create).to receive(:new)
.and_return(double(:UserEntities, call: "Some Value"))
end
# controller tests go here
end