Pastebin launched a little side project called HostCabi.net, check it out ;-)Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jan 19th, 2013  |  syntax: Rails  |  size: 5.25 KB  |  hits: 5  |  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. describe UnitsController do
  2.   login_admin
  3.  
  4.   before :each do
  5.     @course = FactoryGirl.create :course
  6.     @unit = FactoryGirl.create :unit, course: @course
  7.   end
  8.  
  9.   # This should return the minimal set of attributes required to create a valid
  10.   # Unit. As you add validations to Unit, be sure to
  11.   # update the return value of this method accordingly.
  12.   def valid_attributes
  13.     { "name" => "MyString", "course_id" => @course.id}
  14.   end
  15.  
  16.   describe "GET index" do
  17.     it "assigns all units as @units" do
  18.       unit = Unit.create! valid_attributes
  19.       get :index, :course_id => @course.id
  20.       assigns(:units).should include(unit)
  21.     end
  22.   end
  23.  
  24.   describe "GET show" do
  25.     it "assigns the requested unit as @unit" do
  26.       unit = Unit.create! valid_attributes
  27.       get :show, :course_id => @course.id, :id => unit.to_param
  28.       assigns(:unit).should eq(unit)
  29.     end
  30.   end
  31.  
  32.   describe "GET new" do
  33.     it "assigns a new unit as @unit" do
  34.       get :new, :course_id => @course.id
  35.       assigns(:unit).should be_a_new(Unit)
  36.     end
  37.   end
  38.  
  39.   describe "GET edit" do
  40.     it "assigns the requested unit as @unit" do
  41.       unit = Unit.create! valid_attributes
  42.       get :edit, :course_id => @course.id, :id => unit.to_param
  43.       assigns(:unit).should eq(unit)
  44.     end
  45.   end
  46.  
  47.   describe "POST create" do
  48.     describe "with valid params" do
  49.       it "creates a new Unit" do
  50.         expect {
  51.           post :create, :course_id => @course.id, :unit => valid_attributes
  52.         }.to change(Unit, :count).by(1)
  53.       end
  54.  
  55.       it "assigns a newly created unit as @unit" do
  56.         post :create, :course_id => @course.id, :unit => valid_attributes
  57.         assigns(:unit).should be_a(Unit)
  58.         assigns(:unit).should be_persisted
  59.       end
  60.  
  61.       it "redirects to the created unit" do
  62.         unit_id = 1
  63.         Unit.any_instance.should_receive(:save).and_return(true)
  64.         Unit.any_instance.stub(:id).and_return(unit_id)
  65.         post :create, :course_id => @course.to_param , :unit => valid_attributes
  66.         response.should redirect_to(course_unit_path(@course, unit_id))
  67.       end
  68.     end
  69.  
  70.     describe "with invalid params" do
  71.       it "assigns a newly created but unsaved unit as @unit" do
  72.         # Trigger the behavior that occurs when invalid params are submitted
  73.         Unit.any_instance.stub(:save).and_return(false)
  74.         post :create, :course_id => @course.id, :unit => { "name" => "invalid value" }
  75.         assigns(:unit).should be_a_new(Unit)
  76.       end
  77.  
  78.       it "re-renders the 'new' template" do
  79.         # Trigger the behavior that occurs when invalid params are submitted
  80.         Unit.any_instance.stub(:save).and_return(false)
  81.         post :create, :course_id => @course.id, :unit => { "name" => "invalid value" }
  82.         response.should render_template("new")
  83.       end
  84.     end
  85.   end
  86.  
  87.   describe "PUT update" do
  88.     describe "with valid params" do
  89.       it "updates the requested unit" do
  90.         unit = Unit.create! valid_attributes
  91.         # Assuming there are no other units in the database, this
  92.         # specifies that the Unit created on the previous line
  93.         # receives the :update_attributes message with whatever params are
  94.         # submitted in the request.
  95.         Unit.any_instance.should_receive(:update_attributes).with({ "name" => "MyString" })
  96.         put :update,  :course_id => @course.to_param, :id => unit.to_param, :unit => { "name" => "MyString" }
  97.       end
  98.  
  99.       it "assigns the requested unit as @unit" do
  100.         unit = Unit.create! valid_attributes
  101.         put :update,  :course_id => @course.to_param, :id => unit.to_param, :unit => valid_attributes
  102.         assigns(:unit).should eq(unit)
  103.       end
  104.  
  105.       it "redirects to the unit" do
  106.         unit = Unit.create! valid_attributes
  107.         put :update, :course_id => @course.to_param ,:id => unit.to_param, :unit => valid_attributes
  108.         response.should redirect_to(course_unit_path(@course, unit))
  109.       end
  110.     end
  111.  
  112.     describe "with invalid params" do
  113.       it "assigns the unit as @unit" do
  114.         unit = Unit.create! valid_attributes
  115.         # Trigger the behavior that occurs when invalid params are submitted
  116.         Unit.any_instance.stub(:save).and_return(false)
  117.         put :update, :course_id => @course.to_param, :id => unit.to_param, :unit => { "name" => "invalid value" }
  118.         assigns(:unit).should eq(unit)
  119.       end
  120.  
  121.       it "re-renders the 'edit' template" do
  122.         unit = Unit.create! valid_attributes
  123.         # Trigger the behavior that occurs when invalid params are submitted
  124.         Unit.any_instance.stub(:save).and_return(false)
  125.         put :update, :course_id => @course.to_param, :id => unit.to_param, :unit => { "name" => "invalid value" }
  126.         response.should render_template("edit")
  127.       end
  128.     end
  129.   end
  130.  
  131.   describe "DELETE destroy" do
  132.     it "destroys the requested unit" do
  133.       unit = Unit.create! valid_attributes
  134.       expect {
  135.         delete :destroy, :course_id => @course.to_param, :id => unit.to_param
  136.       }.to change(Unit, :count).by(-1)
  137.     end
  138.  
  139.     it "redirects to the units list" do
  140.       unit = Unit.create! valid_attributes
  141.       delete :destroy, :course_id => @course.to_param, :id => unit.to_param
  142.       response.should redirect_to(units_url)
  143.     end
  144.   end
  145.  
  146. end