Last active
August 1, 2021 15:28
-
-
Save lynnbright/71ac3641db8d7dd33ef14d98ed1fa449 to your computer and use it in GitHub Desktop.
Use shared_examples to avoid duplicate test examples
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 FruitController < ApplicationController | |
before_action :find_super_market, only: [:index, :show] | |
before_action :setup_discount, only: [:index, :show] | |
def index | |
#... | |
end | |
def show | |
#... | |
end | |
private | |
def find_super_market | |
@super_market = SuperMarket.find_by!(slug: params[:super_market_slug]) | |
end | |
def setup_discount | |
@discount = #.... | |
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
# fruit_controller_spec.rb | |
require 'rails_helper' | |
RSpec.describe FruitController, type: :controller do | |
describe 'GET #index' do | |
subject(:index_request) { get :index, params: { super_market_slug: super_market.slug } } | |
let(:user) { create(:user, special_pass_available: special_pass_available? } | |
#... | |
context 'when assigns @discount' do | |
let(:super_market) { create(:super_market) } | |
before { index_request } | |
context 'when current_user owns special pass' do | |
let(:special_pass_available?) { true } | |
it { expect(assigns(:discount)).to eq(0.5) } | |
end | |
context 'when current_user does not own any special pass' do | |
let(:special_pass_available?) { false } | |
it { expect(assigns(:discount)).to eq(0) } | |
end | |
end | |
end | |
describe 'GET #show' do | |
subject(:show_request) { get :show, params: { super_market_slug: super_market.slug, fruit_id: fruit.id } } | |
let(:user) { create(:user, special_pass_available: special_pass_available? } | |
let(:fruit) { create(:fruit) } | |
#... | |
context 'when assigns @discount' do | |
let(:super_market) { create(:super_market) } | |
before { show_request } | |
context 'when current_user owns special pass' do | |
let(:special_pass_available?) { true } | |
it { expect(assigns(:discount)).to eq(0.5) } | |
end | |
context 'when current_user does not own any special pass' do | |
let(:special_pass_available?) { false } | |
it { expect(assigns(:discount)).to eq(0) } | |
end | |
end | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment