# encoding: UTF-8
require 'spec_helper'

describe <%= controller_class_name %> do
  before :each do
    @administrator = Administrator.find_by_email('administrator@mozo.bar') || Administrator.create(email: 'administrator@mozo.bar', 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 %>)
        @<%= model_name %>.reload
        assigns(:<%= model_name %>).should eq(@<%= model_name %>)
      end

      it "changes @<%= model_name %>'s attributes" do
        put :update, id: @<%= model_name %>, <%= model_name %>: attributes_for(:<%= model_name %>, <%= required_attribute_name %>: "ChangedByTest")
        @<%= model_name %>.reload
        @<%= model_name %>.<%= required_attribute_name %>.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

    context "invalid attributes" do
      it "locates the requested <%= model_name %>" do
        put :update, id: @<%= model_name %>, <%= model_name %>: {<%= required_attribute_name %>: ''}
        assigns(:<%= model_name %>).should eq(@<%= model_name %>)
      end

      it "re-renders the edit method" do
        put :update, id: @<%= model_name %>, <%= model_name %>: {<%= required_attribute_name %>: ''}
        response.should render_template :edit
      end
    end
  end

  describe 'DELETE destroy' do
    before :each do
      @<%= model_name %> = create :<%= model_name %>
    end

    it "deletes the <%= model_name %>" do
      expect{
        delete :destroy, id: @<%= model_name %>        
      }.to change(<%= model_name.classify %>, :count).by(-1)
    end

    it "redirects to <%= model_plural_name %>#index" do
      delete :destroy, id: @<%= model_name %>
      response.should redirect_to <%= controller_namespace_path.present? ? "[#{controller_namespace_path.split('/').map{|n| ":#{n}"}.join(', ')}, :#{model_plural_name}]" : "#{model_plural_name}_path" %>
    end
  end
end
