radar / twist

Book review application for LeanPub's Markdown format
MIT License
103 stars 30 forks source link

Error Sending invitations #20

Closed romenigld closed 7 years ago

romenigld commented 7 years ago

I'm at page 62 and when I try to send an Invitation mailer. The error is different on the ebook and is similar error before that.

rspec spec/features/accounts/inviting_users_spec.rb
F

Failures:

  1) Inviting users invites a user successfully
     Failure/Error: expect(email).to be_present
       expected `nil.present?` to return true, got false

spec/features/accounts/inviting_users_spec.rb

require "rails_helper"

feature "Inviting users" do
  let(:account) { FactoryGirl.create(:account) }

  before do
    set_subdomain(account.subdomain)
    login_as(account.owner)
    visit root_url
  end

  scenario "invites a user successfully" do
    click_link "Invite User"
    fill_in "Email", with: "test@example.com"
    click_button "Invite User"
    expect(page).to have_content("test@example.com has been invited.")
    expect(page.current_url).to eq(root_url)

    email = find_email("test@example.com")
    expect(email).to be_present
    expect(email.subject).to eq("Invitation to join #{account.name} on Twist")
  end
end

app/controllers/accounts/invitations_controller.rb

module Accounts
  class InvitationsController < Accounts::BaseController
    before_action :authorize_owner!

    def new
      @invitation = Invitation.new
    end

    def create
      @invitation = current_account.invitations.new(invitation_params)
      @invitation.save
      InvitationMailer.invite(@invitation).deliver_later
      flash[:notice] = "#{@invitation.email} has been invited."
      redirect_to root_path
    end

    private

    def invitation_params
      params.require(:invitation).permit(:email)
    end

    def authorize_owner!
        unless owner?
          flash[:alert] = "Only an owner of an account can do that."
          redirect_to root_url(subdomain: current_account.subdomain)
        end
    end
  end
end

app/mailers/invitation_mailer.rb

class InvitationMailer < ApplicationMailer
  def invite(invitation)
    @invitation = invitation
    mail(
      to: invitation.email,
      subject: "Invitation to join #{invitation.account.name} on Twist"
    )
  end
end

invitation.rb

class Invitation < ApplicationRecord
  belongs_to :account

  validates :email, presence: true
end

account.rb

class Account < ApplicationRecord
  belongs_to :owner, class_name: "User"
  accepts_nested_attributes_for :owner

  validates :subdomain, presence: true,
                        uniqueness: true
  has_many :invitations                          
end

So I'm checking the code and I notice has an error on the page 61. The path indicated 1st is different.

Let’s add a couple of new lines to the end of the scenario insidespec/features/inviting_users_- spec.rb to check that the specified user receives an email:

and then the ebooks shows for do inside the accounts folder: spec/features/accounts/inviting_users_spec.rb

when I generate the mailer invitation was this:

rails g mailer invitation -p
    conflict  app/mailers/invitation_mailer.rb
Overwrite /Users/romenigld/workspace/ebooks/Multitenancy with Rails 2nd Edition/twist/app/mailers/invitation_mailer.rb? (enter "h" for help) [Ynaqdh] n
        skip  app/mailers/invitation_mailer.rb
      invoke  erb
       exist    app/views/invitation_mailer
   identical    app/views/layouts/mailer.text.erb
   identical    app/views/layouts/mailer.html.erb
      invoke  rspec
   identical    spec/mailers/invitation_spec.rb
   identical    spec/mailers/previews/invitation_preview.rb

So I try to continue with that to create the: app/views/invitation_mailer/invite.html.erb and the error continues. And I notice again on the page 63 the path of the file inviting_users_spec.rb is different:

Let’s see what happens when we run bundle exec rspec spec/features/inviting_users_spec.rb again:

radar commented 7 years ago

This one was failing because I had not setup the ActiveJob adapter at all. I have now done so in 85f7cb71ee8c3de52cdf8cba2121fac95b463c14. Please rebase your branch against master and continue.

radar commented 7 years ago

BTW you can see where I'm up to in the book by looking at the mtwr-walkthrough branch on this repo. I'm currently halfway through chapter 3, just finished the restricting accounts section.