I was looking at the RailsGuides and I saw this example:
require 'test_helper'
class UserFlowsTest < ActionDispatch::IntegrationTest
test "login and browse site" do
# login via https
https!
get "/login"
assert_response :success
post_via_redirect "/login", username: users(:david).username, password: users(:david).password
assert_equal '/welcome', path
assert_equal 'Welcome david!', flash[:notice]
https!(false)
get "/articles/all"
assert_response :success
assert assigns(:articles)
end
end
I am confused about the variable "path" in the line assert_equal '/welcome', path. What is this exactly? Is this the url that would show up in your browser after we perform the post_via_redirect action right before it?
I looked into the helpers available in the integration tests, but the document didn't say anything about this "path" variable.
Additionally, I try it at my rails app and it works fine, but when I tried to call it at one of my controller tests, it gave me and undefined local variables or method error. So does it mean that the "path" variable is only available in the integration tests? What if I want to use it in the other types of tests?
path is the url of your current page.
path is request.url without the domain name.
In this test you've been redirected from '/login' to '/welcome'.
Related
I'm trying to run a rake test using devise (using a user that's been created by database seeds)
Running rake test produces the following error:
Failure:
CustomersControllerTest#test_should_get_index [/home/ubuntu/workspace/test/controllers/customers_controller_test.rb:7]:
Expected response to be a <2XX: success>, but was a <302: Found> redirect to <http://www.example.com/users/sign_in>
Response body: <html><body>You are being redirected.</body></html>
My test looks like this:
test "should get index" do
get customers_url
assert_response :success
end
It's a very simple test, but I don't know how to actually login to the site before performing the test.
To test actions that require an authenticated user you can use Devise's sign_in and sign_out helpers. These come from Devise::Test::ControllerHelpers on your test case or its parent class (or Devise::Test::IntegrationHelpers for Rails 5+):
class CustomersControllerTest < ActionController::TestCase
include Devise::Test::ControllerHelpers # <-- Include helpers
test "should get index" do
sign_in User.create(...) # <-- Create and authenticate a user
get customers_url
assert_response :success
end
end
See the controller tests section in the Devise README for details.
I have a request spec that is trying to test file download functionality in Rails 3.1 for me. The spec (in part) looks like this:
get document_path(Document.first)
logger(response.body)
response.should be_success
It fails with:
Failure/Error: response.should be_success
expected success? to return true, got false
But if I test the download in the browser, it downloads the file correctly.
Here's the action in the controller:
def show
send_file #document.file.path, :filename => #document.file_file_name,
:content_type => #document.file_content_type
end
And my logger gives this information about the response:
<html><body>You are being redirected.</body></html>
How can I get this test to pass?
Update:
As several pointed out, one of my before_filters was doing the redirect. The reason is that I was using Capybara to login in the test, but not using it's methods for navigating around the site. Here's what worked (partially):
click_link 'Libraries'
click_link 'Drawings'
click_link 'GS2 Drawing'
page.response.should be_success #this still fails
But now I can't figure out a way to test the actual response was successful. What am I doing wrong here.
Most likely, redirect_to is being called when you run your test. Here's what I would do to determine the cause.
Add logging to any before filters that could possibly run for this action.
Add logging at several points in the action itself.
This will tell you how far execution gets before the redirect. Which in turn will tell you what block of code (probably a before_filter) is redirecting.
If I had to take a guess off the top of my head, I'd say you have a before_filter that checks if the user is logged in. If that's true, then you'll need to make sure your tests create a logged-in session before you call the login-protected action.
I was getting the same redirect until I realized that my login(user) method was the culprit. Cribbed from this SO link, I changed my login method to:
# file: spec/authentication_helpers.rb
module AuthenticationHelpers
def login(user)
post_via_redirect user_session_path, 'user[email]' => user.email, 'user[password]' => user.password
end
end
In my tests:
# spec/requests/my_model_spec.rb
require 'spec_helper'
require 'authentication_helpers'
describe MyModel do
include AuthenticationHelpers
before(:each) do
#user = User.create!(:email => 'user#email.com', :password => 'password', :password_confirmation => 'password')
login(#user)
end
it 'should run your integration tests' do
# your code here
end
end
[FWIW: I'm using Rails 3.0, Devise, CanCan and Webrat]
I have written a functional test that changes some of the request object's environment variables to simulate a user has logged in.
require 'test_helper'
class BeesControllerTest < ActionController::TestCase
# See that the index page gets called correctly.
def test_get_index
#request.env['HTTPS'] = "on"
#request.env['SERVER_NAME'] = "sandbox.example.com"
#request.env['REMOTE_USER'] = "joeuser" # Authn/Authz done via REMOTE_USER
get :index
assert_response :success
assert_not_nil(assigns(:bees))
assert_select "title", "Bees and Honey"
end
end
The functional test works fine.
Now I want to do something similar as part of integration testing. Here is what I tried:
require 'test_helper'
class CreateBeeTest < ActionController::IntegrationTest
fixtures :bees
def test_create
#request.env['HTTPS'] = "on"
#request.env['SERVER_NAME'] = "sandbox.example.com"
#request.env['REMOTE_USER'] = "joeuser" # Authn/Authz done via REMOTE_USER
https?
get "/"
assert_response :success
[... more ...]
end
end
I get an error complaining that #request is nil. I suspect this has something to do with the session object, but I am not sure how to get it to work.
You can set HTTPS in integration tests with
https!
And set the host name with:
host! "sandbox.example.com"
Which may be equivalent to what you want to do?
This is described in the Rails guides Rails guides
You can change request variables via parameters to post method.
For your case, method test_create will be:
def test_create
https!
get "/", nil, { 'SERVER_NAME'] => "sandbox.example.com", 'REMOTE_USER'] => "joeuser" }
assert_response :success
[... more ...]
end
Same works for setting post request to raw data:
post root_path, nil, { 'RAW_POST_DATA' => 'some string' }
Does anyone know how to make rspec follow a redirect (in a controller spec)? (e.g test/unit has follow_redirect!)
I have tried "follow_redirect!" and "follow_redirect" but only get
undefined method `follow_redirect!' for #<Spec::Rails::Example::ControllerExampleGroup::Subclass_1:0xb6df5294>
For example:
When I create an account the page is redirected to accounts page and my new account should be at the top of the list.
it "should create an account" do
post :create, :name => "My New Account"
FOLLOW_REDIRECT!
response.code.should == "200"
accounts = assigns[:accounts]
accounts[0].name.should == "My New Account"
end
But FOLLOW_REDIRECT! needs to be changed to something that actually works.
I think this is the default behavior for rspec-rails controller tests, in the sense that you can set an expectation on the response status and/or path, and test for success.
For example:
it "should create an account" do
post :create
response.code.should == "302"
response.should redirect_to(accounts_path)
end
You can access the redirect location with
response.headers['Location']
you could then request that directly.
If you want to test the redirect you are moving outside of the rspec-rails domain.
You can use Webrat or some other integration-test framework to test this.
The easiest way to solve this without resorting to integration testing is probably to mock out the method that is causing the redirect.
The spec is out of scope, if you want to follow a redirect use request spec, the equivalent of integration test in Test::Unit.
In request specs follow_redirect! works as well as in Test::Unit.
Or if you want to redirect inmediately use _via_redirect as suffix for the verb, example:
post_via_redirect :create, user: #user
Try to use integration/request tests. They are using web-like acces through routing to controllers.
For example:
I have for Rails 2 app in file /spec/integration/fps_spec.rb
require 'spec_helper'
describe "FinPoradci" do
it "POST /fps.html with params" do
fp_params={:accord_id => "FP99998", :under_acc => "OM001", :first_name => "Pavel", :last_name => "Novy"}
fp_test=FinPoradce.new(fp_params)
#after create follow redirection to show
post_via_redirect "/fps", {:fp => fp_params}
response.response_code.should == 200 # => :found , not 302 :created
new_fp=assigns(:fp)
new_fp.should_not be_empty
new_fp.errors.should be_empty
flash[:error].should be_empty
flash[:notice].should_not be_empty
response.should render_template(:show)
end
end
and it works. Until you want to send headers (for basic http authorization).
env={'HTTP_AUTHORIZATION' => ActionController::HttpAuthentication::Basic.encode_credentials(user,password)}
post_via_redirect "/fps", {:fp => fp_params}, env
is fine for create, but after redirection it returns 401 and needs new authorization.
SO I have to split it in 2 tests: creation and show on result of creation.
For RSpec / Capybara + Rails
response_headers['Location']
But it works only if there is no delay before redirect.
If it is there, then it's harder to follow the logic.
I was having a heck of a time figuring out how to login and logout using response objects from Rails. The standard blogs were ok, but I finally diagnosed it, and I wanted to record it here.
app.get '/'
assert_response :success
app.get '/auth_only_url'
assert_response 302
user = User.find(:user_to_login)
app.post '/signin_url',
:user_email => user.email,
:user_password => '<password in clear>'
assert_response 302
app.follow_redirect!
assert_response :success
app.get '/auth_only_url'
assert_response :success
Note, the above implies that you redirect after a failed auth request, and also that you redirect after logging in.
To ensure that you load the fixtures into your test environment DB (which normally occurs during rake test), make sure you execute the following:
rake db:fixtures:load RAILS_ENV=test
(From Patrick Richie)
The default URL will appear to be 'www.example.com', as this default host as set in ActionController::Integration::Session
ActionController::Integration::Session.new.host=> "www.example.com"
It is set in actionpack/lib/action_controller/integration.rb#75
To change it in the integration test, do the following:
session = open_session do |s| s.host = 'my-example-host.com' end
'www.example.com' is the default host as set in ActionController::Integration::Session
>> ActionController::Integration::Session.new.host
=> "www.example.com"
It is set in actionpack/lib/action_controller/integration.rb#75
You should be able to change it in your integration test by doing the following:
session = open_session do |s|
s.host = 'my-example-host.com'
end