Rspec controller specs with pundit authorization

134 views
Skip to first unread message

pandur...@greycampus.com

unread,
Aug 24, 2016, 7:59:41 AM8/24/16
to rspec
Hi There

I'm using pundit for authorization and rspec for testing in my rails app. I wrote some controller tests those are failing due to pundit admin role authorization. How can make them pass? Please help me, any suggestion will be appreciated.

Here is my controller specs file.


require 'rails_helper'
require 'rails-controller-testing'

RSpec.describe Admin::PostsController, :type => :controller do
 
# This should return the minimal set of attributes required to create a valid
 
# Product. As you add validations to Product, be sure to
 
# adjust the attributes here as well.
  let
(:valid_attributes) {
   
FactoryGirl.attributes_for(:post)
 
}

 let(:invalid_attributes) {
   
{ title: nil}
 
}

 # This should return the minimal set of values that should be in the session
 
# in order to pass any filters (e.g. authentication) defined in
 
# Admin::ProductsController. Be sure to keep this updated too.
  let
(:valid_session) { {} }

 describe "GET index" do
    it
"assigns all posts as @posts" do
      post
= Post.create! valid_attributes
     
get :index, {}, valid_session
      expect
(assigns(:posts)).to eq([post])
   
end
 
end

 describe "GET show" do
    it
"assigns the requested post as @post" do
      post
= Post.create! valid_attributes
     
get :show, {:id => post.to_param}, valid_session
      expect
(assigns(:post)).to eq(post)
   
end
 
end

 describe "GET new" do
    it
"assigns a new post as @post" do
     
get :new, {}, valid_session
      expect
(assigns(:post)).to be_a_new(Post)
   
end
 
end

 describe "GET edit" do
    it
"assigns the requested post as @post" do
      post
= Post.create! valid_attributes
     
get :edit, {:id => post.to_param}, valid_session
      expect
(assigns(:post)).to eq(post)
   
end
 
end

 describe "POST create" do
    describe
"with valid params" do
      it
"creates a new Post" do
        expect
{
          post
:create, {:post => valid_attributes}, valid_session
       
}.to change(Post, :count).by(1)
     
end

     it "assigns a newly created post as @post" do
        post
:create, {:post => valid_attributes}, valid_session
        expect
(assigns(:post)).to be_a(Post)
        expect
(assigns(:post)).to be_persisted
     
end

     it "redirects to the index page after success" do
        post
:create, {:post => valid_attributes}, valid_session
        expect
(response).to redirect_to(admin_posts_path)
     
end
   
end
 
end
end



Thanks in advance.


Reply all
Reply to author
Forward
0 new messages