Commit 44571b3d

mo khan <mo@mokhan.ca>
2013-07-27 01:38:52
fix creation controllers specs
1 parent 12832c1
Changed files (2)
app/controllers/creations_controller.rb
@@ -20,7 +20,7 @@ class CreationsController < ApplicationController
   end
 
   def create
-    @creation = current_user.creations.create(params[:creation])
+    @creation = current_user.creations.create(creation_params)
     @creation.categories << Category.find(params[:category_id]) if params[:category_id]
     current_user.tag(@creation, :with => params[:creation_tags], :on => :tags)
 
@@ -39,11 +39,11 @@ class CreationsController < ApplicationController
     @creation.categories << Category.find(params[:category_id]) if params[:category_id]
     current_user.tag(@creation, :with => params[:creation_tags], :on => :tags)
 
-    if @creation.update_attributes(params[:creation])
+    if @creation.update_attributes(creation_params)
       redirect_to new_creation_photo_url(@creation)
     else
       flash[:error] = @creation.errors.full_messages
-      render :action => "edit" 
+      render :edit
     end
   end
 
@@ -55,4 +55,10 @@ class CreationsController < ApplicationController
   def mine
     @creations = current_user.creations.includes([:user]).page(params[:page]).per(12)
   end
+
+  private
+
+  def creation_params
+    params.require(:creation).permit(:name, :story, :is_restricted, :watermark)
+  end
 end
spec/controllers/creations_controller_spec.rb
@@ -1,16 +1,16 @@
 require 'spec_helper'
 
 describe CreationsController do
-  let(:user){ FactoryGirl.create(:user) }
-  let(:creation){ FactoryGirl.create(:creation, :user => user) }
+  let(:user){ create(:user) }
+  let(:creation){ create(:creation, :user => user) }
 
   before(:each) do
     photo = File.new(File.join(Rails.root, 'spec/fixtures/images/example.png'))
     creation.add_photo(photo)
   end
 
-  describe "GET index" do
-    let(:restricted_creation){ FactoryGirl.create(:creation, :user => user, :is_restricted => true) }
+  describe :index do
+    let(:restricted_creation){ create(:creation, :user => user, :is_restricted => true) }
 
     before { get :index }
 
@@ -26,89 +26,94 @@ describe CreationsController do
   context "when logged in" do
     before { http_login(user) }
 
-    describe "GET show" do
-      it "assigns the requested creation as @creation" do
+    describe :show do
+      it "assigns the requested creation" do
         get :show, :id => creation.id
-        assigns(:creation).should eq(creation)
+        assigns(:creation).should == creation
       end
     end
 
-    describe "GET new" do
-      it "assigns a new creation as @creation" do
-        new_creation = fake
-        Creation.stub(:new) { new_creation }
+    describe :new do
+      it "assigns a new creation" do
+        new_creation = double
+        Creation.stub(:new).and_return(new_creation)
         get :new
         assigns(:creation).should be(new_creation)
       end
     end
 
-    describe "GET edit" do
+    describe :edit do
       it "assigns the requested creation as @creation" do
         get :edit, :id => creation.id
         assigns(:creation).should eq(creation)
       end
     end
 
-    describe "POST create" do
+    describe :post do
       describe "with valid params" do
+        let(:category) { create(:category) }
+
         before :each do
-          creations = fake
-          user.stub(:creations).and_return(creations)
-          creations.stub(:create).and_return(creation)
-          post :create, :creation => {:id => creation.id, :name => 'new name'}
+          post :create, :creation => { :name => 'stone', :story => 'morning glory', :is_restricted => true, :watermark => "watery"}, :creation_tags => 'cake', :category_id => category.id
         end
 
-        it "assigns a newly created creation as @creation" do
-          assigns(:creation).should eq(creation)
+        it "assigns a newly created creation" do
+          assigns(:creation).should_not be_nil
+          assigns(:creation).name.should == 'stone'
+          assigns(:creation).story.should == 'morning glory'
+          assigns(:creation).is_restricted.should be_true
+          assigns(:creation).watermark.should == 'watery'
         end
 
         it "redirects to the created creation" do
-          response.should redirect_to(new_creation_photo_path(creation))
+          response.should redirect_to(new_creation_photo_path(assigns(:creation)))
         end
       end
 
       describe "with invalid params" do
-        before :each do
-          post :create, :creation => {:name => ''}
-        end
+        before { post :create, :creation => {:name => ''} }
+
         it "re-renders the 'new' template" do
           response.should render_template("new")
         end
+
         it "should include the errors" do
-          assigns(:creation).errors.any?.should be_true
+          assigns(:creation).errors.count.should > 0
         end
       end
     end
 
-    describe "PUT update" do
+    describe :patch do
       describe "with valid params" do
-        before :each do
-          put :update, :id => creation.id, :creation => {:name => 'params'}
-        end
+        before { patch :update, :id => creation.id, :creation => {:name => 'params'} }
+
         it "assigns the requested creation as @creation" do
-          assigns(:creation).should eq(creation)
+          assigns(:creation).should == creation
         end
 
         it "redirects to the creation" do
-          response.should redirect_to("/creations/#{creation.id}-params/photos/new")
+          response.should redirect_to new_creation_photo_path(creation.reload)
         end
       end
 
       describe "with invalid params" do
-        before :each do
-          put :update, :id => creation.id, :creation => {:name=> nil }
-        end
+        before { put :update, :id => creation.id, :creation => {:name=> nil } }
+
         it "assigns the creation as @creation" do
-          assigns(:creation).should eq(creation)
+          assigns(:creation).should == creation
         end
 
         it "re-renders the 'edit' template" do
           response.should render_template("edit")
         end
+
+        it "should display an error" do
+          flash[:error].should_not be_nil
+        end
       end
     end
 
-    describe "DELETE destroy" do
+    describe :destroy do
       before :each do
         delete :destroy, :id => creation.id
       end
@@ -123,8 +128,8 @@ describe CreationsController do
     end
 
     describe :mine do
-      let!(:my_creation) { FactoryGirl.create(:creation) }
-      let!(:other_creation) { FactoryGirl.create(:creation) }
+      let!(:my_creation) { create(:creation) }
+      let!(:other_creation) { create(:creation) }
 
       before :each do
         user.creations << my_creation