Commit 1360517

Stephen Peasley <s@stephenpeasley.com>
2015-01-19 02:48:38
Add tests for registration email
1 parent 72b4a52
app/mailers/user_mailer.rb
@@ -1,4 +1,7 @@
 class UserMailer < ApplicationMailer
   def registration_email(user)
+    @name = user.name
+    @username = user.username
+    mail to: user.email, subject: "Welcome to Supply."
   end
 end
app/views/user_mailer/registration_email.html.erb
@@ -0,0 +1,7 @@
+<html>
+  <body>
+    <p><%= "Hi, #{@username}." %></p>
+    <%= yield %>
+    <p>The Supply Team</p>
+  </body>
+</html>
\ No newline at end of file
app/views/user_mailer/registration_email.text.erb
@@ -0,0 +1,5 @@
+<%= "Hi, #{@username}." %>
+
+<%= yield %>
+
+The Supply Team
\ No newline at end of file
spec/controllers/registrations_controller_spec.rb
@@ -21,45 +21,45 @@ describe RegistrationsController do
         post :create, user: { username: username, password: password, email: email, terms_and_conditions: true }
       end
 
-      it 'creates a new user account' do
+      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
+      it "redirects them to the dashboard" do
         expect(response).to redirect_to(dashboard_path)
       end
 
-      it 'logs them in' do
+      it "logs them in" do
         expect(session[:user_id]).to eql(User.first.id)
       end
 
-      it 'does not display any errors' do
+      it "does not display any errors" do
         expect(flash[:error]).to be_nil
       end
 
-      it 'sends a user registration email' do
+      it "sends a user registration email" do
         expect(mailer).to have_received(:deliver_later)
       end
     end
 
     context "when the parameters provided are invalid" do
       before :each do
-        post :create, user: { username: '', password: password, email: email, terms_and_conditions: true }
+        post :create, user: { username: "", password: password, email: email, terms_and_conditions: true }
       end
 
-      it 'adds an error to the flash for missing usernames' do
+      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
+      it "does not log them in" do
         expect(session[:user_id]).to be_nil
       end
 
-      it 'renders the registration page' do
+      it "renders the registration page" do
         expect(response).to render_template(:new)
       end
     end
spec/mailers/user_mailer_spec.rb
@@ -1,5 +1,20 @@
 require "rails_helper"
 
 RSpec.describe UserMailer, :type => :mailer do
-  pending "add some examples to (or delete) #{__FILE__}"
-end
+  describe "registration email" do
+    let(:user) { double User, name:"Blah", username: "blah", email:"blah@example.com" }
+    let(:mail) { UserMailer.registration_email(user) }
+    it "renders the subject" do
+      expect(mail.subject).to eql("Welcome to Supply.")
+    end
+    it "renders the recipient email" do
+      expect(mail.to).to eql([user.email])
+    end
+    it "renders the sender email" do
+      expect(mail.from).to eql(["from@example.com"])
+    end
+    it "assigns a username" do
+      expect(mail.body.encoded).to match(user.username)
+    end
+  end
+end
\ No newline at end of file