require 'spec_helper' describe PlacesController 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 # PlacesController. Be sure to keep this updated too. def valid_session {} end let!(:place) { FactoryGirl.create(:place) } let(:valid_attributes) { FactoryGirl.attributes_for(:place) } describe "GET index" do it "assigns all places as @places" do get :index, {}, valid_session assigns(:places).should eq([place]) end end describe "GET show" do it "assigns the requested place as @place" do get :show, {:id => place.to_param}, valid_session assigns(:place).should eq(place) 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 place as @place" do get :new, {} assigns(:place).should be_a_new(Place) 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 => place.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 place as @place" do get :edit, {:id => place.to_param} assigns(:place).should eq(place) 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 Place" do expect { post :create, {:place => valid_attributes} }.to change(Place, :count).by(1) end it "assigns a newly created place as @place" do post :create, {:place => valid_attributes} assigns(:place).should be_a(Place) assigns(:place).should be_persisted end it "redirects to the created place" do post :create, {:place => valid_attributes} response.should redirect_to(Place.last) end end describe "with invalid params" do it "assigns a newly created but unsaved place as @place" do # Trigger the behavior that occurs when invalid params are submitted Place.any_instance.stub(:save).and_return(false) post :create, {:place => { "street_address" => "invalid value" }} assigns(:place).should be_a_new(Place) end it "re-renders the 'new' template" do # Trigger the behavior that occurs when invalid params are submitted Place.any_instance.stub(:save).and_return(false) post :create, {:place => { "street_address" => "invalid value" }} 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 => place.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 place" do # Assuming there are no other places in the database, this # specifies that the Place created on the previous line # receives the :update_attributes message with whatever params are # submitted in the request. Place.any_instance.should_receive(:update_attributes).with({ "street_address" => "MyString" }) put :update, {:id => place.to_param, :place => { "street_address" => "MyString" }} end it "assigns the requested place as @place" do put :update, {:id => place.to_param, :place => valid_attributes} assigns(:place).should eq(place) end it "redirects to the place" do put :update, {:id => place.to_param, :place => valid_attributes} response.should redirect_to(place) end end describe "with invalid params" do it "assigns the place as @place" do # Trigger the behavior that occurs when invalid params are submitted Place.any_instance.stub(:save).and_return(false) put :update, {:id => place.to_param, :place => { "street_address" => "invalid value" }} assigns(:place).should eq(place) end it "re-renders the 'edit' template" do # Trigger the behavior that occurs when invalid params are submitted Place.any_instance.stub(:save).and_return(false) put :update, {:id => place.to_param, :place => { "street_address" => "invalid value" }} 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 => place.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 place" do expect { delete :destroy, {:id => place.to_param} }.to change(Place, :count).by(-1) end it "redirects to the places list" do delete :destroy, {:id => place.to_param} response.should redirect_to(places_url) end end end end