Created
January 19, 2015 20:01
-
-
Save KamilLelonek/eabe5b9db97f91e61dd9 to your computer and use it in GitHub Desktop.
Repository pattern spec example in Ruby for Rails developers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class BrandInMemoryAdapter | |
attr_accessor :db | |
def initialize | |
@db = [] | |
end | |
def all | |
db | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
RSpec.describe QueryObjects::GetAllBrands do | |
let(:adapter) { BrandInMemoryAdapter.new } | |
let(:repository) { Repositories::BrandRepository.new(adapter) } | |
let(:subject) { described_class.new(repository) } | |
context 'No records in database' do | |
it 'fetches all brands' do | |
expect(subject.()).to eq [] | |
end | |
end | |
context 'Prefilled records in database' do | |
let(:dummy_records) do | |
10.times.map do | |
OpenStruct.new( | |
id: SecureRandom.uuid, | |
name: Faker::Name.name, | |
image: "#{Faker::Internet.http_url}/image.png", | |
description: Faker::Lorem.paragraph, | |
) | |
end | |
end | |
let(:index) { rand(dummy_records.length) } | |
before { adapter.db = dummy_records } | |
it 'fetches all brands' do | |
record = subject.()[index] | |
expect(record.id) .to eq dummy_records[index].id | |
expect(record.name) .to eq dummy_records[index].name | |
expect(record.image) .to eq dummy_records[index].image | |
expect(record.description).to eq dummy_records[index].description | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment