require 'spec_helper'

describe FetprofilesController, :type => :controller  do
  let(:valid_update_attributes) { {"vorname"=>"Neuer Vorname"}  }
  #  let(:valid_session) { {locale: :de} }

  def self.create_fetprofile
    before(:each) do
      @fetprofile = FactoryGirl.create(:fetprofile)    
    end
  end
  def self.create_active_fetprofile
    before(:each) do
      @fetprofile = FactoryGirl.create(:fetprofile, active: true)    
    end
  end

  let(:object_variable) {:fetprofile}    
  let(:assigned_object_variable) {@fetprofile}
  let(:object_class) {Fetprofile}

  describe "GET index" do
    create_active_fetprofile
    subject(:action) {  get :index, {} }
    let(:object_variable) {:fetprofiles}    
    let(:assigned_object_variable) {[@fetprofile]}
    it_behaves_like "it is success"
    it_behaves_like "it assigns object"  
  end

  describe "GET show" do
    create_fetprofile
    subject(:action) { get :show, {:id => @fetprofile.to_param}}
    it_behaves_like "it is success"
    it_behaves_like "it assigns object"
      it "renders the 'show' template" do
        expect(action).to render_template("show")
      end
      it "assigns memberships" do
        action
        expect(assigns(:memberships)).to be_a(ActiveRecord::Relation)
      end

  end

  describe "GET new" do
    subject(:action) { get :new, {}}
    describe "with fetuser" do
      login_fet_user
      it_behaves_like "it assigns new object"
      it_behaves_like "it is success"  
    end
    describe "without user" do 
      logout_user
      it_behaves_like "it is restricted"
    end 

  end

  describe "GET edit" do

    subject(:action) {get :edit, :id=>@fetprofile.to_param}
    login_fet_user
    create_fetprofile
    it_behaves_like "it is success"
    it_behaves_like "it assigns object"
      it "assigns memberships" do
        action
        expect(assigns(:memberships)).to be_a(ActiveRecord::Relation)
      end

    it "assigns the requested fetprofile as @fetprofile" do
      expect{action}.to change {assigns(:fetprofile)}.to(@fetprofile)
 #     expect(assigns(:fetprofile)).to eq(@fetprofile)
    end
    describe "without user" do 
      logout_user
      it_behaves_like "it is restricted"
    end 
    
  end

  describe "POST create" do
    subject(:action){post :create, {:fetprofile => FactoryGirl.attributes_for(:fetprofile),:format=>:html}}

    login_fet_user
    describe "with valid params" do

      it "has a 302 status code" do
        expect(action.status).to eq(302)
      end      
      it "creates a new Fetprofile" do
        expect{action}.to change(Fetprofile, :count).by(1)
      end
      
      it_behaves_like "it assigns persisted object"

      it "assigns memberships" do
        action
        expect(assigns(:memberships)).to be_a(ActiveRecord::Relation)
      end

      it "redirects to the created fetprofile" do
        expect(action).to redirect_to assigns(:fetprofile)
      end
    end

    describe "with invalid params" do
      has_invalid_params
      it "assigns memberships" do
        action
        expect(assigns(:memberships)).to be_a(ActiveRecord::Relation)
      end

      it "assigns a newly created but unsaved fetprofile as @fetprofile" do
        action
        assigns(:fetprofile).should be_a_new(Fetprofile)
        expect(assigns(:fetprofile)).not_to be_persisted
      end
      it "re-renders the 'new' template" do
        expect(action).to render_template("new")
      end
    end
    describe "without user" do 
      logout_user
      it_behaves_like "it is restricted"
    end 

  end

  describe "PUT update" do
    let(:action) { put :update, {:id => @fetprofile.to_param, :fetprofile => {"vorname"=>"neuerName"}} }
    
    create_fetprofile
    login_fet_user

    describe "with valid params" do
      it_behaves_like "it assigns object"

      it "updates the requested fetprofile" do
       expect { action }.to change{Fetprofile.find(@fetprofile.id).vorname}.to("neuerName")
      end
      it_behaves_like "it assigns persisted object"

      it "redirects to the fetprofile" do
        expect(action).to redirect_to(@fetprofile)
      end
    end

    describe "with invalid params" do
      has_invalid_params
      it_behaves_like "it assigns object"
      it "re-renders the 'edit' template" do
        expect(action).to render_template("edit")
      end
    end
    describe "without user" do 
      logout_user
      it_behaves_like "it is restricted"
    end 
  end

  describe "DELETE destroy" do
    login_fet_user
    create_fetprofile
    subject(:action) { delete :destroy, {:id => @fetprofile.to_param}}

    it "destroys the requested fetprofile" do
      expect {
       action
      }.to change(Fetprofile, :count).by(-1)
    end

    it "redirects to the fetprofiles list" do
      action
      response.should redirect_to(fetprofiles_url)
    end
    describe "without user" do 
      logout_user
      it_behaves_like "it is restricted"
    end 

  end

end
