require 'spec_helper'

module Handcart
  describe SubdomainsController do
    routes { Handcart::Engine.routes }

    let(:valid_attributes) { attributes_for(:handcart_subdomain) }
    let(:valid_session) { {} }

    describe "GET index" do
      it "assigns all subdomains as @subdomains" do
        subdomain = Subdomain.create! valid_attributes
        get :index, {}, valid_session
        assigns(:subdomains).should eq([subdomain])
      end
    end

    describe "GET show" do
      it "assigns the requested subdomain as @subdomain" do
        subdomain = Subdomain.create! valid_attributes
        get :show, {id: subdomain.to_param}, valid_session
        assigns(:subdomain).should eq(subdomain)
      end
    end

    describe "GET new" do
      it "assigns a new subdomain as @subdomain" do
        get :new, {}, valid_session
        assigns(:subdomain).should be_a_new(Subdomain)
      end
    end

    describe "GET edit" do
      it "assigns the requested subdomain as @subdomain" do
        subdomain = Subdomain.create! valid_attributes
        get :edit, {id: subdomain.to_param}, valid_session
        assigns(:subdomain).should eq(subdomain)
      end
    end

    describe "POST create" do
      describe "with valid params" do
        it "creates a new Subdomain" do
          expect {
            post :create, {subdomain: valid_attributes}, valid_session
          }.to change(Subdomain, :count).by(1)
        end

        it "assigns a newly created subdomain as @subdomain" do
          post :create, {subdomain: valid_attributes}, valid_session
          assigns(:subdomain).should be_a(Subdomain)
          assigns(:subdomain).should be_persisted
        end

        it "redirects to the subdomains index" do
          post :create, {subdomain: valid_attributes}, valid_session
          response.should redirect_to(subdomains_url)
        end
      end

      describe "with invalid params" do
        it "assigns a newly created but unsaved subdomain as @subdomain" do
          # Trigger the behavior that occurs when invalid params are submitted
          Subdomain.any_instance.stub(:save).and_return(false)
          post :create, {subdomain: { "name" => "invalid value" }}, valid_session
          assigns(:subdomain).should be_a_new(Subdomain)
        end

        it "re-renders the 'new' template" do
          # Trigger the behavior that occurs when invalid params are submitted
          Subdomain.any_instance.stub(:save).and_return(false)
          post :create, {subdomain: { "name" => "invalid value" }}, valid_session
          response.should render_template("new")
        end
      end
    end

    describe "PUT update" do
      describe "with valid params" do
        it "updates the requested subdomain" do
          subdomain = Subdomain.create! valid_attributes
          # Assuming there are no other subdomains in the database, this
          # specifies that the Subdomain created on the previous line
          # receives the :update_attributes message with whatever params are
          # submitted in the request.
          Subdomain.any_instance.should_receive(:update).with({ "name" => "MyString" })
          put :update, {id: subdomain.to_param, subdomain: { "name" => "MyString" }}, valid_session
        end

        it "assigns the requested subdomain as @subdomain" do
          subdomain = Subdomain.create! valid_attributes
          put :update, {id: subdomain.to_param, subdomain: valid_attributes}, valid_session
          assigns(:subdomain).should eq(subdomain)
        end

        it "redirects to the subdomains index" do
          subdomain = Subdomain.create! valid_attributes
          put :update, {id: subdomain.to_param, subdomain: valid_attributes}, valid_session
          response.should redirect_to(subdomains_url)
        end
      end

      describe "with invalid params" do
        it "assigns the subdomain as @subdomain" do
          subdomain = Subdomain.create! valid_attributes
          # Trigger the behavior that occurs when invalid params are submitted
          Subdomain.any_instance.stub(:save).and_return(false)
          put :update, {id: subdomain.to_param, subdomain: { "name" => "invalid value" }}, valid_session
          assigns(:subdomain).should eq(subdomain)
        end

        it "re-renders the 'edit' template" do
          subdomain = Subdomain.create! valid_attributes
          # Trigger the behavior that occurs when invalid params are submitted
          Subdomain.any_instance.stub(:save).and_return(false)
          put :update, {id: subdomain.to_param, subdomain: { "name" => "invalid value" }}, valid_session
          response.should render_template("edit")
        end
      end
    end

    describe "DELETE destroy" do
      it "destroys the requested subdomain" do
        subdomain = Subdomain.create! valid_attributes
        expect {
          delete :destroy, {id: subdomain.to_param}, valid_session
        }.to change(Subdomain, :count).by(-1)
      end

      it "redirects to the subdomains list" do
        subdomain = Subdomain.create! valid_attributes
        delete :destroy, {id: subdomain.to_param}, valid_session
        response.should redirect_to(subdomains_url)
      end
    end

  end
end
