Commit 8fbd848

mo <mo.khan@gmail.com>
2018-09-01 23:45:25
display validation errors on registration.
1 parent bfea5f2
Changed files (2)
app/controllers/registrations_controller.rb
@@ -10,6 +10,8 @@ class RegistrationsController < ApplicationController
   def create
     User.create!(user_params)
     redirect_to new_session_path
+  rescue ActiveRecord::RecordInvalid => invalid
+    redirect_to new_registration_path, error: invalid.record.errors.full_messages
   end
 
   private
spec/requests/registrations_spec.rb
@@ -1,24 +1,31 @@
 require 'rails_helper'
 
-RSpec.describe RegistrationsController do
-  describe "#new" do
-    it 'renders a registration page' do
-      get '/registrations/new'
+RSpec.describe '/registrations' do
+  describe "GET /registrations/new" do
+    before { get '/registrations/new' }
 
-      expect(response).to have_http_status(:ok)
-      expect(response.body).to include("Register")
-    end
+    specify { expect(response).to have_http_status(:ok) }
+    specify { expect(response.body).to include("Register") }
   end
 
-  describe "#create" do
-    let(:email) { FFaker::Internet.email }
+  describe "POST /registrations" do
+    context "when the new registration data is valid" do
+      let(:email) { FFaker::Internet.email }
+      before { post "/registrations", params: { user: { email: email, password: "password" } } }
+
+      specify { expect(response).to redirect_to(new_session_url) }
+      specify { expect(User.count).to eql(1) }
+      specify { expect(User.last.email).to eql(email) }
+    end
+
+    context "when the new registration data is invalid" do
+      let(:user) { create(:user) }
+      let(:email) { user.email }
 
-    it 'registers a new user' do
-      post "/registrations", params: { user: { email: email, password: "password" } }
+      before { post "/registrations", params: { user: { email: email, password: "password" } } }
 
-      expect(response).to redirect_to(new_session_url)
-      expect(User.count).to eql(1)
-      expect(User.last.email).to eql(email)
+      specify { expect(response).to redirect_to(new_registration_path) }
+      specify { expect(flash[:error]).to be_present }
     end
   end
 end