Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby on Rails functional test with Devise authentication

I'm searching for a solution for a weird problem. I have a controller, that needs authentication (with the devise gem). I added the Devise TestHelpers but i can't get it working.

require 'test_helper'

class KeysControllerTest < ActionController::TestCase  
   include Devise::TestHelpers  
   fixtures :keys

   def setup
      @user = User.create!(
        :email => '[email protected]',
        :password => 'MyTestingPassword',
        :password_confirmation => 'MyTestingPassword'
      )
      sign_in @user
      @key = keys(:one)
   end

   test "should get index" do
      get :index    
      assert_response :success
      assert_not_nil assigns(:keys)
   end

   test "should get new" do
      get :new
      assert_response :success
   end

   test "should create key" do
      assert_difference('Key.count') do
         post :create, :key => @key.attributes
      end

      assert_redirected_to key_path(assigns(:key))
   end

   test "should destroy key" do
      assert_difference('Key.count', -1) do
         delete :destroy, :id => @key.to_param
      end

      assert_redirected_to keys_path
   end

end

And i get the following output in my "rake test" window:

29) Failure:
test_should_create_key(KeysControllerTest) [/test/functional/keys_controller_test.rb:29]:
"Key.count" didn't change by 1.
<3> expected but was
<2>.

 30) Failure:
test_should_destroy_key(KeysControllerTest) [/test/functional/keys_controller_test.rb:37]:
"Key.count" didn't change by -1.
<1> expected but was
<2>.

 31) Failure:
test_should_get_index(KeysControllerTest) [/test/functional/keys_controller_test.rb:19]:
Expected response to be a <:success>, but was <302>

 32) Failure:
test_should_get_new(KeysControllerTest) [/test/functional/keys_controller_test.rb:25]:
Expected response to be a <:success>, but was <302>

Can someone tell my, why devise doesn't authenticate? I'm using the exact same procedure for an AdminController and it works perfect.

like image 415
axx Avatar asked Apr 10 '11 15:04

axx


People also ask

How do I run a test in Ruby on Rails?

2.7 The Rails Test Runner Or we can run a single test file by passing the bin/rails test command the filename containing the test cases. This will run all test methods from the test case. You can also run a particular test method from the test case by providing the -n or --name flag and the test's method name.

Does devise work with Rails 7?

Our out-of-the box Devise setup is now working with Rails 7. Once again, if you'd like to refer to any of the code for this setup, or use the template wholesale for a new app, the code is available on GitHub, and you may also use it as a template repo to kick off your own Rails 7 devise projects.

How do you run a Minitest?

Setting Up Minitest. To run a Minitest test, the only setup you really need is to require the autorun file at the beginning of a test file: require 'minitest/autorun' . This is good if you'd like to keep the code small. A better way to get started with Minitest is to have Bundler create a template project for you.


2 Answers

Are you using Devise with confirmable? In this case, create is not enough and you need to confirm the user with @user.confirm!

Second, why do you create the user in the functional test? Declare your users in the fixture like this (confirmed_at if you require confirmation only):

test/fixtures/users.yml:

user1: 
id: 1
email: [email protected]
encrypted_password: abcdef1
password_salt:  efvfvffdv
confirmed_at: <%= Time.now %>

and sign them in in your functional tests with:

sign_in users(:user1)

Edit: I just saw, that in my app the Devise-Testhelpers are declared in test/test-helpers.rb and I don't know if this makes a difference, maybe you want to try:

ENV["RAILS_ENV"] = "test"
require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'

class ActionController::TestCase
  include Devise::TestHelpers
end

class ActiveSupport::TestCase
  # Setup all fixtures in test/fixtures/*.(yml|csv) for all tests in alphabetical order.
  #
  # Note: You'll currently still have to declare fixtures explicitly in integration tests
  # -- they do not yet inherit this setting
  fixtures :all

  # Add more helper methods to be used by all tests here...
end
like image 106
Markus Proske Avatar answered Oct 23 '22 03:10

Markus Proske


This took me some time to figure out but it turns out the answer is really simple.

The point is that, in your fixtures file (users.yml), you need to make sure the user is 'confirmed' (assuming that you specified "confirmable" in your User model). So, for instance, put this in your users.yml:

user_one:
  confirmed_at: 2015/01/01

That's all, no need to specify other fields (email, encrypted password, etc).

Now in your controller test (e.g. in 'setup' or in 'before') you simply code:

sign_in users(:user_one)

And then it should just work!

like image 1
leo Avatar answered Oct 23 '22 03:10

leo