This week only. Pastebin PRO Accounts Christmas Special! Don't miss out!Want more features on Pastebin? Sign Up, it's FREE!
Guest

places_controller_spec.rb

By: endorama on May 22nd, 2013  |  syntax: Ruby  |  size: 5.87 KB  |  views: 15  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. require 'spec_helper'
  2.  
  3. describe PlacesController do
  4.  
  5.   # This should return the minimal set of values that should be in the session
  6.   # in order to pass any filters (e.g. authentication) defined in
  7.   # PlacesController. Be sure to keep this updated too.
  8.   def valid_session
  9.     {}
  10.   end
  11.  
  12.   let!(:place) { FactoryGirl.create(:place) }
  13.   let(:valid_attributes) { FactoryGirl.attributes_for(:place) }
  14.  
  15.   describe "GET index" do
  16.     it "assigns all places as @places" do
  17.       get :index, {}, valid_session
  18.       assigns(:places).should eq([place])
  19.     end
  20.   end
  21.  
  22.   describe "GET show" do
  23.     it "assigns the requested place as @place" do
  24.       get :show, {:id => place.to_param}, valid_session
  25.       assigns(:place).should eq(place)
  26.     end
  27.   end
  28.  
  29.   describe "GET new" do
  30.     context "when user is not logged in" do
  31.       it "redirect to the sign in page" do
  32.         get :new, {}, valid_session
  33.         page.should redirect_to user_session_path
  34.       end
  35.     end
  36.  
  37.     context "when user is signed in" do
  38.       login_user
  39.      
  40.       it "assigns a new place as @place" do
  41.         get :new, {}
  42.         assigns(:place).should be_a_new(Place)
  43.       end
  44.     end
  45.   end
  46.  
  47.   describe "GET edit" do
  48.     context "when user is not logged in" do
  49.       it "redirect to the sign in page" do
  50.         get :edit, {:id => place.to_param}, valid_session
  51.         page.should redirect_to user_session_path
  52.       end
  53.     end
  54.  
  55.     context "when user is logged in" do
  56.       login_user
  57.  
  58.       it "assigns the requested place as @place" do
  59.         get :edit, {:id => place.to_param}
  60.         assigns(:place).should eq(place)
  61.       end
  62.     end
  63.   end
  64.  
  65.   describe "POST create" do
  66.     context "when user is not logged in" do
  67.       it "redirect to the sign in page" do
  68.         post :create, {}, valid_session
  69.         page.should redirect_to user_session_path
  70.       end
  71.     end
  72.  
  73.     context "when user is logged in" do
  74.       login_user
  75.  
  76.       describe "with valid params" do
  77.         it "creates a new Place" do
  78.           expect {
  79.             post :create, {:place => valid_attributes}
  80.           }.to change(Place, :count).by(1)
  81.         end
  82.  
  83.         it "assigns a newly created place as @place" do
  84.           post :create, {:place => valid_attributes}
  85.           assigns(:place).should be_a(Place)
  86.           assigns(:place).should be_persisted
  87.         end
  88.  
  89.         it "redirects to the created place" do
  90.           post :create, {:place => valid_attributes}
  91.           response.should redirect_to(Place.last)
  92.         end
  93.       end
  94.  
  95.       describe "with invalid params" do
  96.         it "assigns a newly created but unsaved place as @place" do
  97.           # Trigger the behavior that occurs when invalid params are submitted
  98.           Place.any_instance.stub(:save).and_return(false)
  99.           post :create, {:place => { "street_address" => "invalid value" }}
  100.           assigns(:place).should be_a_new(Place)
  101.         end
  102.  
  103.         it "re-renders the 'new' template" do
  104.           # Trigger the behavior that occurs when invalid params are submitted
  105.           Place.any_instance.stub(:save).and_return(false)
  106.           post :create, {:place => { "street_address" => "invalid value" }}
  107.           response.should render_template("new")
  108.         end
  109.       end
  110.     end
  111.   end
  112.  
  113.   describe "PUT update" do
  114.     context "when user is not logged in" do
  115.       it "redirect to the sign in page" do
  116.         put :update, {:id => place.to_param}, valid_session
  117.         page.should redirect_to user_session_path
  118.       end
  119.     end
  120.  
  121.     context "when user is logged in" do
  122.       login_user
  123.  
  124.       describe "with valid params" do
  125.         it "updates the requested place" do
  126.           # Assuming there are no other places in the database, this
  127.           # specifies that the Place created on the previous line
  128.           # receives the :update_attributes message with whatever params are
  129.           # submitted in the request.
  130.           Place.any_instance.should_receive(:update_attributes).with({ "street_address" => "MyString" })
  131.           put :update, {:id => place.to_param, :place => { "street_address" => "MyString" }}
  132.         end
  133.  
  134.         it "assigns the requested place as @place" do
  135.           put :update, {:id => place.to_param, :place => valid_attributes}
  136.           assigns(:place).should eq(place)
  137.         end
  138.  
  139.         it "redirects to the place" do
  140.           put :update, {:id => place.to_param, :place => valid_attributes}
  141.           response.should redirect_to(place)
  142.         end
  143.       end
  144.  
  145.       describe "with invalid params" do
  146.         it "assigns the place as @place" do
  147.           # Trigger the behavior that occurs when invalid params are submitted
  148.           Place.any_instance.stub(:save).and_return(false)
  149.           put :update, {:id => place.to_param, :place => { "street_address" => "invalid value" }}
  150.           assigns(:place).should eq(place)
  151.         end
  152.  
  153.         it "re-renders the 'edit' template" do
  154.           # Trigger the behavior that occurs when invalid params are submitted
  155.           Place.any_instance.stub(:save).and_return(false)
  156.           put :update, {:id => place.to_param, :place => { "street_address" => "invalid value" }}
  157.           response.should render_template("edit")
  158.         end
  159.       end
  160.     end
  161.   end
  162.  
  163.   describe "DELETE destroy" do
  164.     context "when user is not logged in" do
  165.       it "redirect to the sign in page" do
  166.         delete :destroy, {:id => place.to_param}, valid_session
  167.         page.should redirect_to user_session_path
  168.       end
  169.     end
  170.  
  171.     context "when user is logged in" do
  172.       login_user
  173.      
  174.       it "destroys the requested place" do
  175.         expect {
  176.           delete :destroy, {:id => place.to_param}
  177.         }.to change(Place, :count).by(-1)
  178.       end
  179.  
  180.       it "redirects to the places list" do
  181.         delete :destroy, {:id => place.to_param}
  182.         response.should redirect_to(places_url)
  183.       end
  184.     end
  185.   end
  186.  
  187. end
clone this paste RAW Paste Data