|
# 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: '[email protected]') } |
|
|
|
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: '[email protected]') } |
|
|
|
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}" => "[email protected]") |
|
obj.reload |
|
expect(obj.public_send(attribute)).to eq("[email protected]") |
|
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}" => "[email protected]") |
|
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 |