Skip to content

Instantly share code, notes, and snippets.

@rafaelcgo
Created January 29, 2015 18:18
Show Gist options
  • Save rafaelcgo/c7dcbf6249c95e17e5ff to your computer and use it in GitHub Desktop.
Save rafaelcgo/c7dcbf6249c95e17e5ff to your computer and use it in GitHub Desktop.
# INDEX
shared_examples 'a index request' do |model|
it "renders the :index view" do
get :index
expect(response).to render_template("index")
end
it "populates an array of #{model.to_s}" do
get :index
obj = create(model.to_s.downcase.to_sym)
expect(assigns(model.to_s.downcase.pluralize.to_sym)).to match_array(model.all)
end
end
shared_examples 'a index request with pagination' do |model, per_page|
per_page ||= Kaminari.config.default_per_page
it "displays only #{per_page} results per page" do
get :index
(per_page*2).times {create(model.to_s.downcase.to_sym)}
expect(assigns(model.to_s.downcase.pluralize.to_sym).count).to eq(per_page)
end
end
shared_examples "a index request with searching by email" do |model|
let(:new_object) { create(model.to_s.downcase.to_sym, email: 'test@odbtecnologia.com.br') }
it "finds by email" do
get :index, term: 'odbtecnologia'
expect(assigns(model.to_s.downcase.pluralize.to_sym)).to eq([new_object])
end
end
shared_examples "a index request with searching by name" do |model|
let(:new_object) { create(model.to_s.downcase.to_sym, name: 'test@odbtecnologia.com.br') }
it "finds by name" do
get :index, term: 'odbtecnologia'
expect(assigns(model.to_s.downcase.pluralize.to_sym)).to eq([new_object])
end
end
# SHOW
shared_examples "a show request" do |model|
it "assigns the requested #{model} to @#{model}" do
obj = create(model.to_s.downcase.to_sym)
get :show, id: obj.id
expect(assigns(model.to_s.downcase.to_sym)).to eq(obj)
end
it "renders the :show template" do
get :show, id: create(model.to_s.downcase.to_sym).id
expect(response).to render_template("show")
end
end
# NEW
shared_examples "a new request" do |model|
it "assigns a new #{model} to @#{model}" do
get :new
expect(assigns(model.to_s.downcase.to_sym)).to be_a_new(model)
end
it "renders the :new template" do
get :new
expect(response).to render_template("new")
end
end
# CREATE
shared_examples "a create request" do |model, path|
context "with valid attributes" do
it "saves the new #{model} in the database" do
expect {
post :create, "#{model.to_s.downcase.to_sym}" => attributes_for(model.to_s.downcase.to_sym)
}.to change(model, :count).by(1)
end
it "redirects to the #{model}s page" do
post :create, "#{model.to_s.downcase.to_sym}" => attributes_for(model.to_s.downcase.to_sym)
expect(response).to redirect_to(send(path))
end
end
context "with invalid attributes" do
it "does not save the new #{model} in the database" do
expect {
post :create, "#{model.to_s.downcase.to_sym}" => attributes_for(:"invalid_#{model.to_s.downcase.to_sym}")
}.to_not change(model, :count)
end
it "re-renders the :new template" do
post :create, "#{model.to_s.downcase.to_sym}" => attributes_for(:"invalid_#{model.to_s.downcase.to_sym}")
expect(response).to render_template("new")
end
end
end
# UPDATE
shared_examples "a update request" do |model, attribute, path|
let(:obj) { create(model.to_s.downcase.to_sym) }
context "valid attributes" do
it "located the requested #{model}" do
put :update, id: obj, "#{model.to_s.downcase.to_sym}" => attributes_for(model.to_s.downcase.to_sym)
expect(assigns(model.to_s.downcase.to_sym)).to eq(obj)
end
it "changes #{model}'s attributes" do
put :update, id: obj, "#{model.to_s.downcase.to_sym}" => attributes_for(model.to_s.downcase.to_sym, "#{attribute}" => "newemail@fottorama.com.br")
obj.reload
expect(obj.public_send(attribute)).to eq("newemail@fottorama.com.br")
end
it "redirects to the updated #{model}" do
put :update, id: obj, "#{model.to_s.downcase.to_sym}" => attributes_for(model.to_s.downcase.to_sym)
expect(response).to redirect_to(send(path))
end
end
context "invalid attributes" do
it "locates the requested #{model}" do
put :update, id: obj, "#{model.to_s.downcase.to_sym}" => attributes_for(:"invalid_#{model.to_s.downcase.to_sym}")
expect(assigns(model.to_s.downcase.to_sym)).to eq(obj)
end
it "does not change #{model}'s attributes" do
put :update, id: obj, "#{model.to_s.downcase.to_sym}" => attributes_for(model.to_s.downcase.to_sym, "#{attribute}" => "newemail@fottorama.com.br")
obj.reload
expect(obj.public_send(attribute)).to_not eq(nil)
end
it "re-renders the edit method" do
put :update, id: obj, "#{model.to_s.downcase.to_sym}" => attributes_for(:"invalid_#{model.to_s.downcase.to_sym}")
expect(response).to render_template("edit")
end
end
end
# DELETE
shared_examples "a delete request" do |model, path|
before(:each) { @obj = create(model.to_s.downcase.to_sym) }
it "deletes the #{model}" do
expect{ delete :destroy, id: @obj.id }.to change(model, :count).by(-1)
end
it "redirects to #{model}s#index" do
delete :destroy, id: @obj.id
expect(response).to redirect_to(send(path))
end
end
require 'rails_helper'
include Devise::TestHelpers
describe Admin::UsersController, type: :controller do
context "when logged in" do
login_admin
describe "GET #index" do
it_behaves_like 'a index request', User
it_behaves_like 'a index request with pagination', User, Kaminari.config.default_per_page
it_behaves_like 'a index request with searching by email', User
it_behaves_like 'a index request with searching by name', User
context "with searching" do
let(:oliveira) { create(:user, name: 'Fottorama Oliveira', email: 'test@odbtecnologia.com.br') }
let(:barbosa) { create(:user, name: 'ODB Barbosa', email: 'rcvb@fottorama.com.br') }
it "finds by name or email" do
get :index, term: 'fottorama'
expect(assigns(:users)).to match_array([barbosa, oliveira])
end
end
end
describe "GET #show" do
it_behaves_like "a show request", User
end
describe "GET #new" do
it_behaves_like "a new request", User
end
describe "POST #create" do
it_behaves_like "a create request", User, :admin_users_path
end
describe 'PUT update' do
it_behaves_like "a update request", User, :email, :admin_users_url
end
describe 'DELETE destroy' do
it_behaves_like "a delete request", User, :admin_users_url
end
end
context "when logged out" do
# before :each do
# login_user
# user_sign_in nil
# end
describe "GET #index" do
it "redirects to new_user_session_path"
# it "redirects to new_user_session_path" do
# pending "still to test"
# user_sign_in nil
# expect(get :index).to redirect_to(new_user_session_path)
# end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment