Commit cbce23d

mo khan <mo@mokhan.ca>
2015-01-03 15:51:35
add specs for validation errors.
1 parent f80e87b
Changed files (2)
app/controllers/registrations_controller.rb
@@ -4,8 +4,14 @@ class RegistrationsController < ApplicationController
   end
 
   def create
-    log_in(User.create!(secure_params))
-    redirect_to dashboard_path
+    user = User.create!(secure_params)
+    if params[:user][:username].blank?
+      flash[:error] = 'blah'
+      render :new
+    else
+      log_in(user)
+      redirect_to dashboard_path
+    end
   end
 
   private
spec/controllers/registrations_controller_spec.rb
@@ -13,24 +13,48 @@ describe RegistrationsController do
     let(:password) {  'password' }
     let(:email) { 'email@example.com' }
 
-    it 'creates a new user account' do
-      post :create, user: { username: username, password: password, email: email, terms_and_conditions: true }
+    context "when valid params are provided" do
+      before :each do
+        post :create, user: { username: username, password: password, email: email, terms_and_conditions: true }
+      end
 
-      expect(User.count).to eql(1)
-      first_user = User.first
-      expect(first_user.username).to eql(username)
-      expect(first_user.email).to eql(email)
-    end
+      it 'creates a new user account' do
+        expect(User.count).to eql(1)
+        first_user = User.first
+        expect(first_user.username).to eql(username)
+        expect(first_user.email).to eql(email)
+      end
+
+      it 'redirects them to the dashboard' do
+        expect(response).to redirect_to(dashboard_path)
+      end
+
+      it 'logs them in' do
+        expect(session[:user_id]).to eql(User.first.id)
+      end
 
-    it 'redirects them to the dashboard' do
-      post :create, user: { username: username, password: password, email: email, terms_and_conditions: true }
-      expect(response).to redirect_to(dashboard_path)
+      it 'does not display any errors' do
+        expect(flash[:error]).to be_nil
+      end
     end
 
-    it 'logs them in' do
-      post :create, user: { username: username, password: password, email: email, terms_and_conditions: true }
+    context "when the parameters provided are invalid" do
+      before :each do
+        post :create, user: { username: '', password: password, email: email, terms_and_conditions: true }
+      end
+
+      it 'adds an error to the flash for missing usernames' do
+        expect(flash[:error]).to_not be_nil
+        expect(flash[:error]).to_not be_empty
+      end
+
+      it 'does not log them in' do
+        expect(session[:user_id]).to be_nil
+      end
 
-      expect(session[:user_id]).to eql(User.first.id)
+      it 'renders the registration page' do
+        expect(response).to render_template(:new)
+      end
     end
   end
 end