describe UnitsController do login_admin before :each do @course = FactoryGirl.create :course @unit = FactoryGirl.create :unit, course: @course end # This should return the minimal set of attributes required to create a valid # Unit. As you add validations to Unit, be sure to # update the return value of this method accordingly. def valid_attributes { "name" => "MyString", "course_id" => @course.id} end describe "GET index" do it "assigns all units as @units" do unit = Unit.create! valid_attributes get :index, :course_id => @course.id assigns(:units).should include(unit) end end describe "GET show" do it "assigns the requested unit as @unit" do unit = Unit.create! valid_attributes get :show, :course_id => @course.id, :id => unit.to_param assigns(:unit).should eq(unit) end end describe "GET new" do it "assigns a new unit as @unit" do get :new, :course_id => @course.id assigns(:unit).should be_a_new(Unit) end end describe "GET edit" do it "assigns the requested unit as @unit" do unit = Unit.create! valid_attributes get :edit, :course_id => @course.id, :id => unit.to_param assigns(:unit).should eq(unit) end end describe "POST create" do describe "with valid params" do it "creates a new Unit" do expect { post :create, :course_id => @course.id, :unit => valid_attributes }.to change(Unit, :count).by(1) end it "assigns a newly created unit as @unit" do post :create, :course_id => @course.id, :unit => valid_attributes assigns(:unit).should be_a(Unit) assigns(:unit).should be_persisted end it "redirects to the created unit" do unit_id = 1 Unit.any_instance.should_receive(:save).and_return(true) Unit.any_instance.stub(:id).and_return(unit_id) post :create, :course_id => @course.to_param , :unit => valid_attributes response.should redirect_to(course_unit_path(@course, unit_id)) end end describe "with invalid params" do it "assigns a newly created but unsaved unit as @unit" do # Trigger the behavior that occurs when invalid params are submitted Unit.any_instance.stub(:save).and_return(false) post :create, :course_id => @course.id, :unit => { "name" => "invalid value" } assigns(:unit).should be_a_new(Unit) end it "re-renders the 'new' template" do # Trigger the behavior that occurs when invalid params are submitted Unit.any_instance.stub(:save).and_return(false) post :create, :course_id => @course.id, :unit => { "name" => "invalid value" } response.should render_template("new") end end end describe "PUT update" do describe "with valid params" do it "updates the requested unit" do unit = Unit.create! valid_attributes # Assuming there are no other units in the database, this # specifies that the Unit created on the previous line # receives the :update_attributes message with whatever params are # submitted in the request. Unit.any_instance.should_receive(:update_attributes).with({ "name" => "MyString" }) put :update, :course_id => @course.to_param, :id => unit.to_param, :unit => { "name" => "MyString" } end it "assigns the requested unit as @unit" do unit = Unit.create! valid_attributes put :update, :course_id => @course.to_param, :id => unit.to_param, :unit => valid_attributes assigns(:unit).should eq(unit) end it "redirects to the unit" do unit = Unit.create! valid_attributes put :update, :course_id => @course.to_param ,:id => unit.to_param, :unit => valid_attributes response.should redirect_to(course_unit_path(@course, unit)) end end describe "with invalid params" do it "assigns the unit as @unit" do unit = Unit.create! valid_attributes # Trigger the behavior that occurs when invalid params are submitted Unit.any_instance.stub(:save).and_return(false) put :update, :course_id => @course.to_param, :id => unit.to_param, :unit => { "name" => "invalid value" } assigns(:unit).should eq(unit) end it "re-renders the 'edit' template" do unit = Unit.create! valid_attributes # Trigger the behavior that occurs when invalid params are submitted Unit.any_instance.stub(:save).and_return(false) put :update, :course_id => @course.to_param, :id => unit.to_param, :unit => { "name" => "invalid value" } response.should render_template("edit") end end end describe "DELETE destroy" do it "destroys the requested unit" do unit = Unit.create! valid_attributes expect { delete :destroy, :course_id => @course.to_param, :id => unit.to_param }.to change(Unit, :count).by(-1) end it "redirects to the units list" do unit = Unit.create! valid_attributes delete :destroy, :course_id => @course.to_param, :id => unit.to_param response.should redirect_to(units_url) end end end