After 3 years of procrastination today is the day that I start testing my Rails apps. My first step is to fix the failing tests in my Rails 3 beta4 app.
My last 3 failing tests have to do with the devise gem and its authenticate_user! method in a before_filter at the top of my controller.
You'd earn great karma by helping me out with this since it will enable me to use the TDD methodology from now on.
Here is the error that troubles me:
1) Error:
test_should_get_accepted(ModerationControllerTest):
NoMethodError: undefined method `authenticate!' for nil:NilClass
/test/functional/moderation_controller_test.rb:10:in `test_should_get_accepted'
Devise just gives functional tests pointers and helpers in this page: http://github.com/plataformatec/devise but I just don't know how to put this into application.
Can you please give this testing noob some detailed instructions on how to use these helpers?
We can run all of our tests at once by using the bin/rails test command. 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.
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.
What is Minitest? Minitest is a testing tool for Ruby that provides a complete suite of testing facilities. It also supports behaviour-driven development, mocking and benchmarking.
It took me a while but I found the way. Here it is for anyone stuck at the same point:
At the top of the moderation_controller_test.rb, below the class declaration, add this line:
include Devise::TestHelpers
I have 2 records in my user fixture and I added this line within each test where the user has to be authorized to perform the action.
sign_in User.first
Of course it's dead simple once you know how to do it.
If you want the Devise test helpers to be available to all of your tests, you have to enclose the include mentioned by allesklar at the bottom of test_helper.rb
in a class declaration like this:
class ActionController::TestCase
include Devise::TestHelpers
end
Update: 01.25.2017
... rails 5 posts a DEPRECATION WARNING & asks you use ...
Devise::Test::ControllerHelpers
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With