Skip to content

Instantly share code, notes, and snippets.

@apux
Last active December 24, 2015 08:39
Show Gist options
  • Save apux/6772334 to your computer and use it in GitHub Desktop.
Save apux/6772334 to your computer and use it in GitHub Desktop.
require 'spec_helper'
<% module_namespacing do -%>
describe <%= controller_class_name %>Controller do
let(:valid_attributes) { <%= formatted_hash(example_valid_attributes) %> }
let(:valid_session) { {} }
<% unless options[:singleton] -%>
describe "GET index" do
it "asigna todos los <%= table_name.pluralize %> como @<%= table_name.pluralize %>" do
<%= file_name %> = <%= class_name %>.create! valid_attributes
get :index, {}, valid_session
assigns(:<%= table_name %>).should eq([<%= file_name %>])
end
end
<% end -%>
describe "GET show" do
it "asigna el <%= ns_file_name %> solicitado como @<%= ns_file_name %>" do
<%= file_name %> = <%= class_name %>.create! valid_attributes
get :show, {:id => <%= file_name %>.to_param}, valid_session
assigns(:<%= ns_file_name %>).should eq(<%= file_name %>)
end
end
describe "GET new" do
it "asigna un nuevo <%= ns_file_name %> como @<%= ns_file_name %>" do
get :new, {}, valid_session
assigns(:<%= ns_file_name %>).should be_a_new(<%= class_name %>)
end
end
describe "GET edit" do
it "asigna el <%= ns_file_name %> solicitado como @<%= ns_file_name %>" do
<%= file_name %> = <%= class_name %>.create! valid_attributes
get :edit, {:id => <%= file_name %>.to_param}, valid_session
assigns(:<%= ns_file_name %>).should eq(<%= file_name %>)
end
end
describe "POST create" do
describe "con parámetros válidos" do
it "crea un nuevo <%= class_name %>" do
expect {
post :create, {:<%= ns_file_name %> => valid_attributes}, valid_session
}.to change(<%= class_name %>, :count).by(1)
end
it "asigna un <%= ns_file_name %> recién creado como @<%= ns_file_name %>" do
post :create, {:<%= ns_file_name %> => valid_attributes}, valid_session
assigns(:<%= ns_file_name %>).should be_a(<%= class_name %>)
assigns(:<%= ns_file_name %>).should be_persisted
end
it "redirige al <%= ns_file_name %> creado" do
post :create, {:<%= ns_file_name %> => valid_attributes}, valid_session
response.should redirect_to(<%= class_name %>.last)
end
end
describe "con parámetros inválidos" do
it "asigna un <%= ns_file_name %> recién creado sin guardar como @<%= ns_file_name %>" do
# Trigger el behavior that occurs when invalid params are submitted
<%= class_name %>.any_instance.stub(:save).and_return(false)
post :create, {:<%= ns_file_name %> => <%= formatted_hash(example_invalid_attributes) %>}, valid_session
assigns(:<%= ns_file_name %>).should be_a_new(<%= class_name %>)
end
it "re-renderea el template 'new'" do
# Trigger el behavior that occurs when invalid params are submitted
<%= class_name %>.any_instance.stub(:save).and_return(false)
post :create, {:<%= ns_file_name %> => <%= formatted_hash(example_invalid_attributes) %>}, valid_session
response.should render_template("new")
end
end
end
describe "PUT update" do
describe "con parámetros válidos" do
it "actualiza el <%= ns_file_name %> solicitado" do
<%= file_name %> = <%= class_name %>.create! valid_attributes
# Assuming there are no other <%= table_name %> in el database, this
# specifies that el <%= class_name %> created on el previous line
# receives el :update_attributes message with whatever params are
# submitted in el request.
<%- if ::Rails::VERSION::STRING >= '4' -%>
<%= class_name %>.any_instance.should_receive(:update).with(<%= formatted_hash(example_params_for_update) %>)
<%- else -%>
<%= class_name %>.any_instance.should_receive(:update_attributes).with(<%= formatted_hash(example_params_for_update) %>)
<%- end -%>
put :update, {:id => <%= file_name %>.to_param, :<%= ns_file_name %> => <%= formatted_hash(example_params_for_update) %>}, valid_session
end
it "asigna el <%= ns_file_name %> solicitado como @<%= ns_file_name %>" do
<%= file_name %> = <%= class_name %>.create! valid_attributes
put :update, {:id => <%= file_name %>.to_param, :<%= ns_file_name %> => valid_attributes}, valid_session
assigns(:<%= ns_file_name %>).should eq(<%= file_name %>)
end
it "redirige al <%= ns_file_name %>" do
<%= file_name %> = <%= class_name %>.create! valid_attributes
put :update, {:id => <%= file_name %>.to_param, :<%= ns_file_name %> => valid_attributes}, valid_session
response.should redirect_to(<%= file_name %>)
end
end
describe "con parámetros inválidos" do
it "asigna el <%= ns_file_name %> como @<%= ns_file_name %>" do
<%= file_name %> = <%= class_name %>.create! valid_attributes
# Trigger el behavior that occurs when invalid params are submitted
<%= class_name %>.any_instance.stub(:save).and_return(false)
put :update, {:id => <%= file_name %>.to_param, :<%= ns_file_name %> => <%= formatted_hash(example_invalid_attributes) %>}, valid_session
assigns(:<%= ns_file_name %>).should eq(<%= file_name %>)
end
it "re-renderea el template 'edit'" do
<%= file_name %> = <%= class_name %>.create! valid_attributes
# Trigger el behavior that occurs when invalid params are submitted
<%= class_name %>.any_instance.stub(:save).and_return(false)
put :update, {:id => <%= file_name %>.to_param, :<%= ns_file_name %> => <%= formatted_hash(example_invalid_attributes) %>}, valid_session
response.should render_template("edit")
end
end
end
describe "DELETE destroy" do
it "elimina el <%= ns_file_name %> solicitado" do
<%= file_name %> = <%= class_name %>.create! valid_attributes
expect {
delete :destroy, {:id => <%= file_name %>.to_param}, valid_session
}.to change(<%= class_name %>, :count).by(-1)
end
it "redirige al listado de <%= table_name %>" do
<%= file_name %> = <%= class_name %>.create! valid_attributes
delete :destroy, {:id => <%= file_name %>.to_param}, valid_session
response.should redirect_to(<%= index_helper %>_url)
end
end
end
<% end -%>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment