Understanding Acceptance Testing
Let’s understand Acceptance Testing. Acceptance tests for your application may require that a test user be logged in. Moreover, to do this in your application you can either sign in the user using capybara by visiting the sign-in URL and entering valid credentials. However, you can stub the logged-in user.
Moreover, signing in via Capybara is perhaps the most ‘correct’ way since we want our tests to be as close to real-world conditions as possible. However, if you have more than a few tests, these few extra actions for each logged in a test can be quite time-consuming. In addition, the alternative is to use warden’s built-in stubbing actions to make your application think that a user is signed in but without all of the overhead of actually signing them in.
Not to mention, to do this, you’ll need to include warden test helpers and turn on test mode. Further, this includes the following at the top of your file, or in a file that you are including. However, if you are using rspec-rails you can place these lines in a file of the spec/support directory.
Also, follow the instructions at the top of the file, spec/rails_helper.rb:
include Warden::Test::Helpers
Warden.test_mode!
You can also write those lines as:
RSpec.configure do |config|
config.include Warden::Test::Helpers
config.before :suite do
Warden.test_mode!
end
end
Then, within your test, you can make a call to the warden helper login_as with a user resource and specifying the :scope => :user to ‘log in’ a test user.
user = FactoryGirl.create(:user)
login_as(user, :scope => :user)
In addition, you will then need to create a corresponding user factory in your factories file (e.g. spec/factories.rb, test/factories.rb). Further, this will look something like this:
FactoryGirl.define do
factory :user do
email ‘[email protected]’
password ‘f4k3p455w0rd’
# if needed
# is_active true
end
end
However, if you have added any fields to your User model, you will need to add these here. For more details see the FactoryGirl docs.
So, if you are using devise’s confirmable module, you will need to ensure that the newly created user also has the ‘confirmed_at’ field filled in
user = FactoryGirl.create(:user)
user.confirmed_at = Time.now
user.save
Moreover, to make sure this works correctly you will need to reset warden after each test. You can do this by calling
Warden.test_reset!
So, to ensure the reset occurs, you can place the command inside an after : example hook, as follows:
RSpec.configure do |config|
config.after :each do
Warden.test_reset!
end
end
If for some reason you need to log out a logged in test user, you can use Warden’s logout helper.
logout(:user)
According to this StackOverflow answer, this will not work with controller tests because of rack middleware. This includes Warden is not invoked when running controller tests. It is good for your Capybara tests, which run through rack. See also the Warden wiki entry about testing, although the examples are for miniTest.
If you’re wondering why we can’t just use Devise’s built in sign_in and sign_out methods, it’s because these require direct access to the request object which is not available while using Capybara. To bundle the functionality of both methods together you can create a helper method.
Capybara-Webkit
If you have trouble using Warden’s login_as method with the capybara-webkit driver, try setting run_callbacks to false in the login_as options struct
user = FactoryGirl.create(:user)
login_as(user, :scope => :user, :run_callbacks => false)
Make your resume stand out and become a Certified Capybara Testing Professional. So, Try free practice tests here!
A great career is just a certification away. So, practice and validate your skills to become a Certified Capybara Testing Professional.