Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing devise with shoulda

I'm having some difficulties in testing devise with shoulda:

2) Error:
test: handle :index logged as admin should redirect to Daily page.
(Admin::DailyClosesControllerTest):
NoMethodError: undefined method `env' for nil:NilClass
devise (1.0.6) [v] lib/devise/test_helpers.rb:52:in
`setup_controller_for_warden'

I have this in my test_helper:

include Devise::TestHelpers

Thoughts ? Thanks in advance,

Cristi

like image 977
morgan freeman Avatar asked May 15 '10 07:05

morgan freeman


People also ask

What is shoulda matchers?

Shoulda Matchers provides Test::Unit and RSpec-compatible one-liners that test common Rails functionality. It helps you write tests that would otherwise be much longer, more complex, and error-prone. Using shoulda-matchers for testing simplifies the entire process.

What is shoulda gem?

Shoulda Matchers is a Ruby testing gem, that provides RSpec- and Minitest-compatible one-liners that test common Rails functionality. These tests would otherwise be much longer, more complex, and error-prone.

How do I test a controller in Ruby on Rails?

The currently accepted way to test rails controllers is by sending http requests to your application and writing assertions about the response. Rails has ActionDispatch::IntegrationTest which provides integration tests for Minitest which is the Ruby standard library testing framework.


2 Answers

include Devise::TestHelpers doesn't go in the test_helper.rb file, but rather inside the scope of the individual testing classes. Just like their README shows:

class ActionController::TestCase
  include Devise::TestHelpers
end
like image 91
Robert Speicher Avatar answered Sep 19 '22 13:09

Robert Speicher


I'm not sure if rspeicher is fully correct, but putting:

class ActionController::TestCase
  include Devise::TestHelpers
end

at the very bottom of test_helper.rb (yes after the END of the class ActiveSupport::TestCase) should work. It has for 3 or 4 projects of mine so far, including one i'm working on today.

You then can use sign_in users(:one) if you are using fixtures, in your tests. Unless shoulda is messing it up?

like image 23
pjammer Avatar answered Sep 19 '22 13:09

pjammer