Files
mozo-backend/lib/generators/bj_controller/spec/templates/controller_spec.rb
T

109 lines
3.7 KiB
Ruby

# encoding: UTF-8
require 'spec_helper'
describe <%= controller_class_name %> do
before :each do
@administrator = Administrator.find_by_email('administrator@qwaiter.com') || Administrator.create(email: 'administrator@qwaiter.com', password: 'secret')
sign_in @administrator
end
describe "GET #index" do
it "populates an array of <%= model_plural_name %>" do
<%= model_name %> = create :<%= model_name %>
get :index
assigns(:<%= model_plural_name %>).should eq([<%= model_name %>])
end
it "should render without errors when no objects are present" do
get :index
expect{ render_template :index }.not_to raise_error
end
it "renders the :index view" do
get :index
response.should render_template :index
end
end
describe "GET #show" do
it "assigns the requested <%= model_name %> to @<%= model_name %>" do
<%= model_name %> = create :<%= model_name %>
get :show, id: <%= model_name %>
assigns(:<%= model_name %>).should eq(<%= model_name %>)
end
it "renders the #show view" do
<%= model_name %> = create :<%= model_name %>
get :show, id: <%= model_name %>
response.should render_template :show
end
end
describe "GET #new" do
it "assigns a new <%= model_name %> to @<%= model_name %>" do
get :new
assigns(:<%= model_name %>).should be_a <%= model_name.classify %>
end
it "renders the #show view" do
get :new
response.should render_template :new
end
end
describe "POST #create" do
context "with valid attributes" do
it "creates a new <%= model_name %>" do
expect{
post :create, <%= model_name %>: attributes_for(:<%= model_name %>)
}.to change(<%= model_name.classify %>, :count).by(1)
end
it "redirects to the new <%= model_name %>" do
post :create, <%= model_name %>: attributes_for(:<%= model_name %>)
response.should redirect_to <%= controller_namespace_path.present? ? "[#{controller_namespace_path.split('/').map{|n| ":#{n}"}.join(', ')}, #{model_name.classify}.last]" : "#{model_name.classify}.last" %>
end
end
context "with invalid attributes" do
it "does not save the new <%= model_name %>" do
expect{
post :create, <%= model_name %>: {}
}.to_not change(<%= model_name.classify %>, :count)
end
it "re-renders the new method" do
post :create, <%= model_name %>: {}
response.should render_template :new
end
end
end
describe 'PUT update' do
before :each do
@<%= model_name %> = create :<%= model_name %>
end
context "valid attributes" do
it "located the requested <%= model_name %>" do
put :update, id: @<%= model_name %>, <%= model_name %>: attributes_for(:<%= model_name %>)
assigns(:<%= model_name %>).should eq(@<%= model_name %>)
end
it "changes @<%= model_name %>'s attributes" do
attributes = attributes_for(:<%= model_name %>)
attribute_to_change = attributes.keys.find{|k| k !~ /_id$/}
attributes[attribute_to_change] = "ChangedByTest"
put :update, id: @<%= model_name %>, <%= model_name %>: attributes
@<%= model_name %>.reload
@<%= model_name %>.send(attribute_to_change).should eq("ChangedByTest")
end
it "redirects to the updated <%= model_name %>" do
put :update, id: @<%= model_name %>, <%= model_name %>: attributes_for(:<%= model_name %>)
response.should redirect_to <%= controller_namespace_path.present? ? "[#{controller_namespace_path.split('/').map{|n| ":#{n}"}.join(', ')}, @#{model_name}]" : "@#{model_name}" %>
end
end
end
end