Want more features on Pastebin? Sign Up, it's FREE!
Guest

training_controller_spec.rb

By: endorama on May 22nd, 2013  |  syntax: Ruby  |  size: 6.74 KB  |  views: 9  |  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 TrainingsController 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.   # TrainingsController. Be sure to keep this updated too.
  8.   def valid_session
  9.     {}
  10.   end
  11.  
  12.   let!(:training) { FactoryGirl.create(:training) }
  13.   let(:valid_attributes) { FactoryGirl.attributes_for(:training) }
  14.   let(:invalid_attributes) {
  15.     FactoryGirl.attributes_for(:training,
  16.       name: nil,
  17.       contact_id: nil,
  18.       city_id: nil,
  19.       place_id: nil,
  20.       owner_id: nil)
  21.   }
  22.  
  23.   describe "GET index" do
  24.     it "assigns all trainings as @trainings" do
  25.       get :index, {}, valid_session
  26.       assigns(:trainings).should eq([training])
  27.     end
  28.  
  29.     describe 'ATOM format' do
  30.       it "returns an ATOM file" do
  31.         get :index, format: :atom
  32.         expect(response.headers['Content-Type']).to have_content 'application/atom+xml'
  33.       end
  34.      
  35.       it 'returns content' do
  36.         get :index, format: :atom
  37.         response.should be_successful
  38.         response.should render_template("index")
  39.       end
  40.     end
  41.  
  42.     describe 'RSS format' do
  43.       it "should redirect to ATOM format" do
  44.         get :index, format: :rss
  45.         page.should redirect_to events_url(:format => :atom)
  46.       end
  47.     end
  48.   end
  49.  
  50.   describe "GET show" do
  51.     it "assigns the requested training as @training" do
  52.       get :show, {:id => training.to_param}, valid_session
  53.       assigns(:training).should eq(training)
  54.     end
  55.   end
  56.  
  57.   describe "GET new" do
  58.     context "when user is not logged in" do
  59.       it "redirect to the sign in page" do
  60.         get :new, {}, valid_session
  61.         page.should redirect_to user_session_path
  62.       end
  63.     end
  64.  
  65.     context "when user is signed in" do
  66.       login_user
  67.  
  68.       it "assigns a new training as @training" do
  69.         get :new, {}
  70.         assigns(:training).should be_a_new(Training)
  71.       end
  72.     end
  73.   end
  74.  
  75.   describe "GET edit" do
  76.     context "when user is not logged in" do
  77.       it "redirect to the sign in page" do
  78.         get :edit, {:id => training.to_param}, valid_session
  79.         page.should redirect_to user_session_path
  80.       end
  81.     end
  82.  
  83.     context "when user is logged in" do
  84.       login_user
  85.  
  86.       it "assigns the requested training as @training" do
  87.         get :edit, {:id => training.to_param}
  88.         assigns(:training).should eq(training)
  89.       end
  90.     end
  91.   end
  92.  
  93.   describe "POST create" do
  94.     context "when user is not logged in" do
  95.       it "redirect to the sign in page" do
  96.         post :create, {}, valid_session
  97.         page.should redirect_to user_session_path
  98.       end
  99.     end
  100.  
  101.     context "when user is logged in" do
  102.       login_user
  103.  
  104.       describe "with valid params" do
  105.         it "creates a new Training" do
  106.           expect {
  107.             post :create, {:training => valid_attributes}
  108.           }.to change(Training, :count).by(1)
  109.         end
  110.  
  111.         it "assigns a newly created training as @training" do
  112.           post :create, {:training => valid_attributes}
  113.           assigns(:training).should be_a(Training)
  114.           assigns(:training).should be_persisted
  115.         end
  116.  
  117.         it "redirects to the created training" do
  118.           post :create, {:training => valid_attributes}
  119.           response.should redirect_to(Training.last)
  120.         end
  121.       end
  122.  
  123.       describe "with invalid params" do
  124.         it "assigns a newly created but unsaved training as @training" do
  125.           # Trigger the behavior that occurs when invalid params are submitted
  126.           Training.any_instance.stub(:save).and_return(false)
  127.           post :create, {:training => invalid_attributes}
  128.           assigns(:training).should be_a_new(Training)
  129.         end
  130.  
  131.         it "re-renders the 'new' template" do
  132.           # Trigger the behavior that occurs when invalid params are submitted
  133.           Training.any_instance.stub(:save).and_return(false)
  134.           post :create, {:training => invalid_attributes}
  135.           response.should render_template("new")
  136.         end
  137.       end
  138.     end
  139.   end
  140.  
  141.   describe "PUT update" do
  142.     context "when user is not logged in" do
  143.       it "redirect to the sign in page" do
  144.         put :update, {:id => training.to_param}, valid_session
  145.         page.should redirect_to user_session_path
  146.       end
  147.     end
  148.  
  149.     context "when user is logged in" do
  150.       login_user
  151.  
  152.       describe "with valid params" do
  153.         it "updates the requested training" do
  154.           # Assuming there are no other trainings in the database, this
  155.           # specifies that the Training created on the previous line
  156.           # receives the :update_attributes message with whatever params are
  157.           # submitted in the request.
  158.           Training.any_instance.should_receive(:update_attributes).with({ "name" => "MyString" })
  159.           put :update, {:id => training.to_param, :training => { "name" => "MyString" }}
  160.         end
  161.  
  162.         it "assigns the requested training as @training" do
  163.           put :update, {:id => training.to_param, :training => valid_attributes}
  164.           assigns(:training).should eq(training)
  165.         end
  166.  
  167.         it "redirects to the training" do
  168.           put :update, {:id => training.to_param, :training => valid_attributes}
  169.           response.should redirect_to(training)
  170.         end
  171.       end
  172.  
  173.       describe "with invalid params" do
  174.         it "assigns the training as @training" do
  175.           # Trigger the behavior that occurs when invalid params are submitted
  176.           Training.any_instance.stub(:save).and_return(false)
  177.           put :update, {:id => training.to_param, :training => invalid_attributes}
  178.           assigns(:training).should eq(training)
  179.         end
  180.  
  181.         it "re-renders the 'edit' template" do
  182.           # Trigger the behavior that occurs when invalid params are submitted
  183.           Training.any_instance.stub(:save).and_return(false)
  184.           put :update, {:id => training.to_param, :training => invalid_attributes}
  185.           response.should render_template("edit")
  186.         end
  187.       end
  188.     end
  189.   end
  190.  
  191.   describe "DELETE destroy" do
  192.     context "when user is not logged in" do
  193.       it "redirect to the sign in page" do
  194.         delete :destroy, {:id => training.to_param}, valid_session
  195.         page.should redirect_to user_session_path
  196.       end
  197.     end
  198.  
  199.     context "when user is logged in" do
  200.       login_user
  201.       it "destroys the requested training" do
  202.         expect {
  203.           delete :destroy, {:id => training.to_param}
  204.         }.to change(Training, :count).by(-1)
  205.       end
  206.  
  207.       it "redirects to the trainings list" do
  208.         delete :destroy, {:id => training.to_param}
  209.         response.should redirect_to(trainings_url)
  210.       end
  211.     end
  212.   end
  213.  
  214. end
clone this paste RAW Paste Data