I use pundit and devise . I have a delete link, which appears only if you are an administrator. I have an integration test that I would like to verify that the delete link is displayed only for administrators.
test 'comment delete link shows when it should' do log_in_as @admin get movie_path(@movie) assert_select 'a[href=?]', movie_comment_path(comments(:one), @movie.id) end
My test_helper.rb looks like this:
... class ActiveSupport::TestCase ... def log_in_as(user, options = {}) password = options[:password] || 'password' if integration_test? post user_session_path, 'user[email]' => user.email, 'user[password]' => user.password else Devise::TestHelpers.sign_in user end end private
response.body looks right, but there really is no deletion link. There is one when I start the development server and visit the page myself. I narrowed it down to current_user , which uses pundit in politics, with a value of nil . This is my comment_policy.rb :
class CommentPolicy attr_reader :current_user, :comment def initialize(current_user, model) @current_user = current_user @comment = model end def create? if @current_user @current_user.member? or @current_user.content_creator? or @current_user.moderator? or @current_user.admin? end end def destroy? if @current_user @current_user == @comment.user or @current_user.moderator? or @current_user.admin? end end end
As a final note, I heard that Rails 5 chose integration tests instead of controller tests, since we know them from Rails 4 for the default test types that will be created for our controllers. If this is the case, then using Rails 5 devise it would be useful to use a lot more if the sign_in / sign_out who work in the controller tests also work with integration tests, But do I still have a question about pundit , not knowing what current_user ? I assume that all this works fine in controller tests, because current_user bound to controllers? Any and all easy shedding on this topic is very appreciated, but I would really like to find out how to get integration tests to work with this installation, because I have about a billion that I want to write right now.