Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
# lib/services/user/signup_service.rb
module Services
module User
class SignUpService
attr_reader :publish_statistics_service,
:send_email_confirmation_service
def initialize(
user_publish_statistics_service:,
user_send_email_confirmation_service:,
)
@publish_statistics_service = user_publish_statistics_service
@send_email_confirmation_service = user_send_email_confirmation_service
end
def call(user)
user.signed_up_at = Time.now
user.save!
publish_user_statistics_service.call(user, 'signup')
send_user_email_confirmation_service.call(user)
end
end
end
end
# spec/services/user/signup_service_spec.rb
require 'spec_helper'
RSpec.describe Services::User::SignupService do
let(:publish_stat_service) { double('publish_stats', call: nil) }
let(:send_email_service) { double('send_email', call: nil) }
let(:service) do
described_class.new(
user_publish_statistics_service: publish_stat_service,
user_send_email_confirmation_service: send_email_service
)
end
describe '.call' do
let(:user) { FactoryGirl.build(:user) }
before do
allow(user).to receive(:save!)
end
it 'sets signed up time' do
expect {
service.call(user)
}.to change(user, :signed_up_at).from(nil)
end
it 'saves user' do
expect(user).to receive(:save!).once
service.call(user)
end
# No need to test that publish stat service is sending stats
# it will be tested in it's own spec file
it 'publishes statistics' do
expect(publish_stat_service).to receive(:call).with(user, 'signup')
service.call(user)
end
# No need to test that send email service is sending email
# it will be tested in it's own spec file
it 'sends confirmation email' do
expect(send_email_service).to receive(:call).with(user)
service.call(user)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.