require 'spec_helper' describe TrainingsController do # This should return the minimal set of values that should be in the session # in order to pass any filters (e.g. authentication) defined in # TrainingsController. Be sure to keep this updated too. def valid_session {} end let!(:training) { FactoryGirl.create(:training) } let(:valid_attributes) { FactoryGirl.attributes_for(:training) } let(:invalid_attributes) { FactoryGirl.attributes_for(:training, name: nil, contact_id: nil, city_id: nil, place_id: nil, owner_id: nil) } describe "GET index" do it "assigns all trainings as @trainings" do get :index, {}, valid_session assigns(:trainings).should eq([training]) end describe 'ATOM format' do it "returns an ATOM file" do get :index, format: :atom expect(response.headers['Content-Type']).to have_content 'application/atom+xml' end it 'returns content' do get :index, format: :atom response.should be_successful response.should render_template("index") end end describe 'RSS format' do it "should redirect to ATOM format" do get :index, format: :rss page.should redirect_to events_url(:format => :atom) end end end describe "GET show" do it "assigns the requested training as @training" do get :show, {:id => training.to_param}, valid_session assigns(:training).should eq(training) end end describe "GET new" do context "when user is not logged in" do it "redirect to the sign in page" do get :new, {}, valid_session page.should redirect_to user_session_path end end context "when user is signed in" do login_user it "assigns a new training as @training" do get :new, {} assigns(:training).should be_a_new(Training) end end end describe "GET edit" do context "when user is not logged in" do it "redirect to the sign in page" do get :edit, {:id => training.to_param}, valid_session page.should redirect_to user_session_path end end context "when user is logged in" do login_user it "assigns the requested training as @training" do get :edit, {:id => training.to_param} assigns(:training).should eq(training) end end end describe "POST create" do context "when user is not logged in" do it "redirect to the sign in page" do post :create, {}, valid_session page.should redirect_to user_session_path end end context "when user is logged in" do login_user describe "with valid params" do it "creates a new Training" do expect { post :create, {:training => valid_attributes} }.to change(Training, :count).by(1) end it "assigns a newly created training as @training" do post :create, {:training => valid_attributes} assigns(:training).should be_a(Training) assigns(:training).should be_persisted end it "redirects to the created training" do post :create, {:training => valid_attributes} response.should redirect_to(Training.last) end end describe "with invalid params" do it "assigns a newly created but unsaved training as @training" do # Trigger the behavior that occurs when invalid params are submitted Training.any_instance.stub(:save).and_return(false) post :create, {:training => invalid_attributes} assigns(:training).should be_a_new(Training) end it "re-renders the 'new' template" do # Trigger the behavior that occurs when invalid params are submitted Training.any_instance.stub(:save).and_return(false) post :create, {:training => invalid_attributes} response.should render_template("new") end end end end describe "PUT update" do context "when user is not logged in" do it "redirect to the sign in page" do put :update, {:id => training.to_param}, valid_session page.should redirect_to user_session_path end end context "when user is logged in" do login_user describe "with valid params" do it "updates the requested training" do # Assuming there are no other trainings in the database, this # specifies that the Training created on the previous line # receives the :update_attributes message with whatever params are # submitted in the request. Training.any_instance.should_receive(:update_attributes).with({ "name" => "MyString" }) put :update, {:id => training.to_param, :training => { "name" => "MyString" }} end it "assigns the requested training as @training" do put :update, {:id => training.to_param, :training => valid_attributes} assigns(:training).should eq(training) end it "redirects to the training" do put :update, {:id => training.to_param, :training => valid_attributes} response.should redirect_to(training) end end describe "with invalid params" do it "assigns the training as @training" do # Trigger the behavior that occurs when invalid params are submitted Training.any_instance.stub(:save).and_return(false) put :update, {:id => training.to_param, :training => invalid_attributes} assigns(:training).should eq(training) end it "re-renders the 'edit' template" do # Trigger the behavior that occurs when invalid params are submitted Training.any_instance.stub(:save).and_return(false) put :update, {:id => training.to_param, :training => invalid_attributes} response.should render_template("edit") end end end end describe "DELETE destroy" do context "when user is not logged in" do it "redirect to the sign in page" do delete :destroy, {:id => training.to_param}, valid_session page.should redirect_to user_session_path end end context "when user is logged in" do login_user it "destroys the requested training" do expect { delete :destroy, {:id => training.to_param} }.to change(Training, :count).by(-1) end it "redirects to the trainings list" do delete :destroy, {:id => training.to_param} response.should redirect_to(trainings_url) end end end end