Last active
August 29, 2015 14:06
-
-
Save tygern/62e4aeafbbbda3ac85af to your computer and use it in GitHub Desktop.
Repositories Provider
This file contains hidden or 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
def repositories(repositories_provider = RepositoriesProvider) | |
@repositories ||= repositories_provider.new() | |
end |
This file contains hidden or 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
config.before(:each, type: :controller) do |example| | |
repositories = controller.send(:repositories, TestRepositoriesProvider) | |
define_singleton_method(:repositories) do | |
repositories | |
end | |
end | |
class TestRepositoriesProvider | |
include RSpec::Mocks::ExampleMethods | |
attr_reader :users | |
def initialize | |
@users = instance_double(UsersRepository) | |
end | |
end |
This file contains hidden or 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 RepositoriesProvider | |
attr_reader :users | |
def initialize | |
@users = UsersRepository.new | |
end | |
end |
This file contains hidden or 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 UsersController < ApplicationController | |
def index | |
@users = repositories.users.find_by_id(params[:id]) | |
end | |
end |
This file contains hidden or 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
require 'rails_helper' | |
describe UsersController do | |
let(:user) { double(:user) } | |
describe 'GET #show' do | |
before do | |
allow(repositories.users).to receive(:find_by_id).and_return(user) | |
end | |
it 'assigns users' do | |
get :show, id: '7' | |
expect(repositories.users).to have_received(:find_by_id).with('7') | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment