I'm new to Rails but not new to software development and so some of these may be elementary questions mixed in with head-scratching ones.
We are trying to get some benchmarking in our app and are having some frustrating moments getting Rails, Capybara, and the database to play well together.
We have a database already filled with data, so I decided to start with that. This is the script I wrote to create and db and run the test:
#!/bin/bash
RAILS_ENV=test
RAILS_ENV=$RAILS_ENV rake db:drop
RAILS_ENV=$RAILS_ENV rake db:create
RAILS_ENV=$RAILS_ENV rake db:migrate
RAILS_ENV=$RAILS_ENV rake demo:data:load
RAILS_ENV=$RAILS_ENV rake test:benchmark
This is the simple test that I wrote:
require 'test_helper'
require 'rails/performance_test_help'
require 'capybara/rails'
# Profiling results for each test method are written to tmp/performance.
class BrowsingTest < ActionDispatch::PerformanceTest
def setup
sign_in "fred"
end
def test_dashboard
get '/dashboard'
get '/question_collections'
get '/question_collections/5'
get '/question_collections/5/questions/60'
get '/message_collections'
end
end
And I get the following error:
NoMethodError: undefined method `sign_in' for
test_dashboard(BrowsingTest):BrowsingTest.test/performance/browsing_test.rb:7:in `setup'
So it seems that Capybara isn't found, which is odd since we use it in our functional tests quite a bit. Then I rolled my own version of the login and came up with:
require 'test_helper'
require 'rails/performance_test_help'
# Profiling results for each test method are written to tmp/performance.
class BrowsingTest < ActionDispatch::PerformanceTest
def setup
login_as
end
def test_dashboard
.....
end
def login_as
open_session do |sess|
user = {}
user["username"]='fred'
user["password"]='*****'
sess.post "/users/sign_in", :user=>user
end
end
end
And that, at least, ran and gave me metrics. But I see this in the log:
Started POST "/users/sign_in" for 127.0.0.1 at 2013-04-23 10:09:54 -0500
Processing by DualLoginController#create as HTML
Parameters: {"user"=>{"username"=>"fred", "password"=>"[FILTERED]"}}
Completed 401 Unauthorized in 24ms
Processing by DualLoginController#new as HTML
Parameters: {"user"=>{"username"=>"fred", "password"=>"[FILTERED]"}}
Rendered devise/shared/_links.erb (1.5ms)
Rendered devise/sessions/new.html.erb within layouts/application (79.6ms)
Completed 200 OK in 277ms (Views: 189.2ms | ActiveRecord: 0.0ms)
Started GET "/dashboard" for 127.0.0.1 at 2013-04-23 10:09:55 -0500
Processing by DashboardController#index as HTML
Completed 401 Unauthorized in 1ms
It seems that the user logged in (maybe) but then wasn't able to get to "/dashboard". The other controllers are the same. If I simply run rake demo:data:load && rails s
I can login with fred
and it works fine.
I'm thinking that that the Capybara issue and the database issue is related -- that, for some reason, rake test:benchmark
isn't running in the Test environment so the data isn't found and Capybara isn't loaded.
Anyone have any thoughts?