3

To preface, I'm extremely new to Rails and general MVC format.

I'm using Rspec to do all my testing and I'm having an issue with this piece of code in my controller_spec:

describe "GET show" do
    context 'with an admin user signed in' do
      with :admin
      let!(:invitation) { create :invitation }   
      before do
        sign_in admin
        get :show, id: invitation.id
      end
      it { should respond_with :ok }
      it { should respond_with_content_type :html }
      it { should render_template :show }
      it "assigns the requested invitation as @invitation" do
        expect(assigns(:invitations)).to eq([invitation])
      end
    end
  end

Here is the error I'm getting:

14) InvitationsController GET show with an admin user signed in assigns the requested invitation as @invitation
     Failure/Error: expect(assigns(:invitations)).to eq([invitation])

       expected: [#<Invitation id: 98, token: "blah", name: "Team", number_of_uses: 5, created_at: "2016-01-29 20:43:27", updated_at: "2016-01-29 20:43:27">]
            got: nil

       (compared using ==)
     # ./spec/controllers/invitations_controller_spec.rb:53:in `block (4 levels) in <top (required)>'

Finally, here are my controller and policy class snippets, respectively.

Controller:

class InvitationsController < ApplicationController
  before_action :set_invitation, only: [:show, :edit, :update, :destroy]

  respond_to :html

  def index
    authorize Invitation
    @invitations = policy_scope(Invitation)
    respond_with(@invitations)
  end

  def show
    authorize @invitation
    respond_with(@invitation)
  end

invitation_policy.rb:

class InvitationPolicy < ApplicationPolicy
  Scope = Struct.new(:user, :scope) do
    def resolve
      Invitation.all
    end
  end

  def index?
    user.admin?
  end

  def show?
    user.admin?
  end

The relevant gems I'm using here are Pundit and FactoryGirl. I'm still learning a lot of what these things mean, so I'm well aware that the answer may be obvious.

1
  • What's your sign_in admin doing? Are you stubbing sign_in? Commented Feb 1, 2016 at 18:07

1 Answer 1

3

Your spec is wrong. It checks that @invitations is set to an array of invitations (which is probably appropriate for the index action) but your show action assigns a single invitation to @invitation.

Sign up to request clarification or add additional context in comments.

1 Comment

Good job spotting that, answers are always simpler than they look.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.