Jump to content

The ultimate community for Ruby on Rails developers.


Photo

rspec, devise, and pundit

rspec devise pundit

  • Please log in to reply
5 replies to this topic

#1 NerdcoreSteve

NerdcoreSteve

    Signalman

  • Members
  • 25 posts

Posted 16 June 2014 - 10:08 PM

In my continued quest to properly test pundit policies I've finally started to read the readme for devise. Namely the bit about test helpers. But I can't seem to get it to work.

 

thing_policy_spec.rb

  1 require 'spec_helper'
  2 include Devise::TestHelpers
  3
  4 describe ThingPolicy do
  5   subject { ThingPolicy.new(user, thing) }
  6   let(:thing) { FactoryGirl.create(:thing) }
  7   sign_in :user
  8
  9   #context "for a visitor" do
 10   #  let(:user) { nil }
 11
 12   #  it { should_not permit(:show) }
 13   #  it { should_not permit(:create) }
 14   #  it { should_not permit(:new) }
 15   #  it { should_not permit(:update) }
 16   #  it { should_not permit(:edit) }
 17   #  it { should_not permit(:destroy) }
 18   #end
 19
 20   #context "for a user" do
 21   #  #TODO add tests here
 22   #end
 23 end

The error I get is

"test_helpers.rb:33:in `warden': undefined method `env' for nil:NilClass (NoMethodError)"



#2 Ohm

Ohm

    Driver

  • Moderators
  • 414 posts
  • LocationCopenhagen

Posted 17 June 2014 - 05:23 AM

As I read the readme, there is no sign_in :user method, it is instead either sign_in @user, or sign_in :user, @user. In your case I would guess that you'd have to create a user with FactoryGirl and then sign in that user with 

let(:user) { FactoryGirl.create(:user) }
sign_in user

Blog: http://ohm.sh | Twitter: @madsohm | Work: Lokalebasen.dk


#3 NerdcoreSteve

NerdcoreSteve

    Signalman

  • Members
  • 25 posts

Posted 17 June 2014 - 05:27 PM

  1 require 'spec_helper'
  2 include Devise::TestHelpers
  3
  4 describe ThingPolicy do
  5   let(:user) { FactoryGirl.create(:user) }
  6   sign_in user
  7 end

gives me "undefined local variable or method `user'"
 

  6   sign_in :user

and

  6   sign_in :user, @user

give me "`warden': undefined method `env' for nil:NilClass"
 

  5   sign_in @user

gives me "`find_scope!': Could not find a valid mapping for nil"

It doesn't matter whether or not the line

let(:user) { FactoryGirl.create(:user) }

is present or not. The output is the same.

For any of these variations, if I include the lines

@request.env["devise.mapping"] = Devise.mappings[:user]
get :new

I always get "undefined method `env' for nil:NilClass"
 

 5   puts @request == nil

gets me "true"

Hopefully when I get more familiar with rails it won't be as hard for me to do simple things like this. :)



#4 NerdcoreSteve

NerdcoreSteve

    Signalman

  • Members
  • 25 posts

Posted 17 June 2014 - 09:08 PM

This works! :) (I think. It passes at any rate)

  1 require 'spec_helper'
  2 require 'database_cleaner'
  3 include Warden::Test::Helpers
  4
  5 describe ThingPolicy do
  6   DatabaseCleaner.strategy = :transaction
  7   DatabaseCleaner.start
  8   @user = FactoryGirl.create(:user)
  9   login_as @user, :scope => :user
 10   DatabaseCleaner.clean
 11 end

Now I need to figure out how to get the database cleaner stuff to work in spec_helper.rb. I thought I had followed the instructions here but I might have missed something.



#5 NerdcoreSteve

NerdcoreSteve

    Signalman

  • Members
  • 25 posts

Posted 17 June 2014 - 10:03 PM

I have now got it to

  1 require 'spec_helper'
  2 include Warden::Test::Helpers
  3
  4 describe ThingPolicy do
  5   @user = FactoryGirl.create(:user)
  6   login_as @user, :scope => :user
  7 end

Now I just want to do the Warden::Test::Helpers include in spec_helper.

 17 RSpec.configure do |config|
 18   #https://github.com/plataformatec/devise
 19   config.include Warden::Test::Helpers

doesn't work...



#6 NerdcoreSteve

NerdcoreSteve

    Signalman

  • Members
  • 25 posts

Posted 17 June 2014 - 10:38 PM   Best Answer

I think I got it!

 

After a little guidance from some local rubyists I typed up this:

  1 require 'spec_helper'
  2 require 'pundit/rspec'
  3
  4 describe ThingPolicy do
  5   subject { ThingPolicy }
  6
  7   let(:user) { FactoryGirl.create(:user) }
  8   let(:thing) { FactoryGirl.create(:thing, user: user) }
  9
 10   permissions :show? do
 11     it 'should allow a user to see things in their own account' do
 12       expect(subject).to permit(user, thing)
 13     end
 14
 15     it 'should not allow a user to see things from other accounts' do
 16       expect(subject).to_not permit(user, FactoryGirl.create(:thing))
 17     end
 18   end
 19 end

This is (I think) exactly what I'm looking for, a simple way to test pundit policies.







Also tagged with one or more of these keywords: rspec, devise, pundit

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users