Skip to content

Instantly share code, notes, and snippets.

@billyyarosh
Last active December 9, 2021 15:58
Show Gist options
  • Save billyyarosh/1a8e062186d3d5bcf0385ada53f9f761 to your computer and use it in GitHub Desktop.
Save billyyarosh/1a8e062186d3d5bcf0385ada53f9f761 to your computer and use it in GitHub Desktop.
Dependency Injection w/ Mocks
RSpec.describe SongService do
require 'spec_helper'
subject { SongService.new(gateway_service: gateway_service) }
let(:song) { Song.new(name: 'Seek Up', artist: 'Dave Matthews Band', genre: 'Rock') }
context 'create_song using mocks' do
class MockSongGatewayService < SongGatewayService
def create_song(song)
song = song.clone
song.id = 'abc'
song.to_json
end
end
let(:gateway_service) { MockSongGatewayService.new }
it 'creates a valid song' do
new_song = subject.create_song(song)
expect(new_song.name).to eq song.name
expect(new_song.artist).to eq song.artist
expect(new_song.genre).to eq song.genre
expect(new_song.id).to_not be_nil
expect(new_song.id).to_not eq song.id
end
it 'raises a validation exception on required name field' do
song.name = nil
expect { subject.create_song(song) }.to raise_error
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment