functional testing issue - ruby-on-rails-3

I have testing code as follow, why "test "should get create" do" always failed????
# called before every single test
def setup
logger.debug '2'
#new_user = User.create(:email => 'shrimpy#email.com',
:password => 'secret',
:password_confirmation => 'secret')
logger.debug "==========> #{#new_user.id}"
end
# called after every single test
def teardown
#new_user.delete
end
test "should get create" do
logger.debug '1'
get :create, :email => "shrimpy#email.com", :password => "secret"
assert_response :success # <------why it always failed here?
assert_redirected_to user_path(#new_user)
end
Console output:
Finished in 3.834383 seconds.
1) Failure:
test_should_get_create(SessionsControllerTest) [test/functional/sessions_controller_test.rb:24]:
Expected block to return true value.
1 tests, 1 assertions, 1 failures, 0 errors, 0 skips
test.log:
2
SQL (2.0ms) SELECT 1 FROM "users" WHERE ("users"."email" = 'shrimpy#email.com') LIMIT 1
SQL (0.0ms) SELECT name
FROM sqlite_master
WHERE type = 'table' AND NOT name = 'sqlite_sequence'
AREL (6.0ms) INSERT INTO "users" ("email", "hashed_password", "salt", "created_at", "updated_at") VALUES ('shrimpy#email.com', 'b4e991c44d9738effa3
98e97d7ed1e6ccad19c90ce2e911344a21bf9c82f915f', '258003960.04544115923816805', '2011-04-21 07:04:00.891929', '2011-04-21 07:04:00.891929')
==========> 980190963
1
Processing by SessionsController#create as HTML
Parameters: {"email"=>"shrimpy#email.com", "password"=>"[FILTERED]"}
User Load (0.0ms) SELECT "users".* FROM "users" WHERE "users"."email" = 'shrimpy#email.com' LIMIT 1
Redirected to http://test.host/users/980190963
Completed 302 Found in 166ms
AREL (0.0ms) DELETE FROM "users" WHERE "users"."id" = 980190963
This is the function i am testing:
def create
if user = User.authenticate(params[:email], params[:password])
session[:user_id] = user.id
redirect_to user_path(user), :notice => 'You have successfully login'
else
redirect_to login_url, :alert => "Invalid email/password combination"
end
end

Because the response isn't success, the response is a redirect.
Understanding this requires a little understanding of HTTP, and a little understanding of what assert_response :success and redirect_to do. All that assert_response :success is testing is the HTTP response code, it's testing to see whether it's a success code. You get a success code when you render a page back to the browser. When you redirect you do not get a success code, you get a redirect code instead.
So if you want to check the response you could use assert_response :redirect - although this is redundant when you already have the assert_redirected_to
Read more here in the Rails guide for testing

Related

why controller var isn't available within a test?

I'm trying to follow Michael Hartl's Ruby on Rails Tutorial in http://ruby.railstutorial.org/chapters/sign-in-sign-out, but with some changes to practice, above all, some variations and the Test::Unit framework. In the tutorial, RSpec is used, while I'm trying to stick to Test::Unit + Shoulda-context.
In chapter 9 I'm suposed to pass some functional tests that use a var called 'controller', but my tests don't work as they find out that 'controller' doesn't exist. This is what I get:
marcel#pua:~/Desenvolupament/Rails3Examples/ror_tutorial$ rake
test:recent Loaded suite
/home/marcel/.rvm/gems/ruby-1.9.2-p290/gems/rake-0.9.2.2/lib/rake/rake_test_loader
Started F
=============================================================================== Failure: test: POST 'create' with valid signin (email and password)
should redirect to the user show page. (SessionsControllerTest)
[test/functional/sessions_controller_test.rb:58]: Expected at least 1
element matching "title", found 0. is not true.
=============================================================================== E
=============================================================================== Error: test: POST 'create' with valid signin (email and password)
should sign in the user. (SessionsControllerTest): NameError:
undefined local variable or method `controller' for
test/functional/sessions_controller_test.rb:53:in `block (3 levels) in <class:SessionsControllerTest>'
=============================================================================== Finished in 0.957865676 seconds. 7 tests, 6 assertions, 1 failures, 1
errors, 0 pendings, 0 omissions, 0 notifications 0% passed
7.31 tests/s, 6.26 assertions/s rake aborted! Command failed with status (1): [/home/marcel/.rvm/rubies/ruby-1.9.2-p290/b...] Tasks: TOP
=> test:recent (See full trace by running task with --trace)
This is the original (RSpec) test:
describe SessionsController do
...
describe "POST 'create'" do
...
describe "with valid email and password" do
before(:each) do
#user = Factory(:user)
#attr = { :email => #user.email, :password => #user.password }
end
it "should sign the user in" do
post :create, :session => #attr
controller.current_user.should == #user
controller.should be_signed_in
end
it "should redirect to the user show page" do
post :create, :session => #attr
response.should redirect_to(user_path(#user))
end
end
end
end
and this is my translated (into Test::Unit + Sholuda-context) test:
class SessionsControllerTest < ActionController::TestCase
context "POST 'create'" do
context "with valid signin (email and password)" do
setup do
#attr = {email: "test#email.tst", password: "testpwd"}
#user=User.create! #attr.merge!({name: "test_user", password_confirmation: "testpwd"})
end
should "sign in the user" do
post :create, :session => #attr
assert_equal #user, controller.current_user
end
should "redirect to the user show page" do
post :create, :session => #attr
assert_select "title", /Show/
end
end
end
end
Has anybody any idea how to make my test work?
Looking at the official Rails testing guide at http://guides.rubyonrails.org/testing.html, I've seen that an instance variable called #controller is enabled in functional tests. so, the Test::Unit version should be:
should "sign in the user" do
post :create, :session => #attr
assert_equal #user, #controller.current_user
end

Statement invalid when attempting to implement password_reset Railscast

I'm attempting to implement Railscast #274 in my app so I can provide a password reset option. I got to the point where I can type my email into a form for password resets and receive an email with a link to reset the password. Things start to go wrong when I enter my new password and attempt to save it. I ended up with an Action Controller:Exception caught. Here is what my log showed after I sent myself an email with the password_reset link:
Started GET "/password_resets/new" for 127.0.0.1 at 2012-01-26 00:50:42 -0500
Processing by PasswordResetsController#new as HTML
Clicking the password_reset link:
Started GET "/password_resets/qlslPnuOhdyMCNseMnV3bA/edit" for 127.0.0.1 at 2012-01-26 00:51:08 -0500
Processing by PasswordResetsController#edit as HTML
Parameters: {"id"=>"qlslPnuOhdyMCNseMnV3bA"}
Adding a new :password and :password_confirmation yields the error:
Started POST "/password_resets/qlslPnuOhdyMCNseMnV3bA" for 127.0.0.1 at 2012-01-26 00:53:08 -0500
Processing by PasswordResetsController#update as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"2egfT2lr35FhuVPWDB72vcS2zPlqC75tcyctRp61ZHw=", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]", "commit"=>"Update Password", "id"=>"qlslPnuOhdyMCNseMnV3bA"}
Started GET "/profiles/qlslPnuOhdyMCNseMnV3bA" for 127.0.0.1 at 2012-01-26 00:53:09 -0500
Processing by ProfilesController#show as HTML
Parameters: {"id"=>"qlslPnuOhdyMCNseMnV3bA"}
Profile Load (0.9ms) SELECT "profiles".* FROM "profiles" WHERE "profiles"."id" = 'qlslPnuOhdyMCNseMnV3bA' LIMIT 1
PGError: ERROR: invalid input syntax for integer: "qlslPnuOhdyMCNseMnV3bA"
LINE 1: ...ofiles".* FROM "profiles" WHERE "profiles"."id" = 'qlslPnuOh...
^
: SELECT "profiles".* FROM "profiles" WHERE "profiles"."id" = 'qlslPnuOhdyMCNseMnV3bA' LIMIT 1
Completed in 57ms
ActiveRecord::StatementInvalid (PGError: ERROR: invalid input syntax for integer: "qlslPnuOhdyMCNseMnV3bA"
LINE 1: ...ofiles".* FROM "profiles" WHERE "profiles"."id" = 'qlslPnuOh...
^
: SELECT "profiles".* FROM "profiles" WHERE "profiles"."id" = 'qlslPnuOhdyMCNseMnV3bA' LIMIT 1):
app/controllers/profiles_controller.rb:41:in `show'
In profiles_controller.rb:41 in show:
def show
#profile = Profile.find(params[:id])
#user = User.find(#profile.user_id)
..
end
Before doing this I dumped my database, ran rake:db create, then rake:db migrate before re-seeded the db. Could this be because I haven't run a script to give existing users a password_reset_token?
UPDATE: Including password_resets_controller.rb:
class PasswordResetsController < ApplicationController
def new
end
def create
#user = #User.find_by_email(params[:email])
if user
user.send_password_reset
redirect_to new_password_reset_path, :notice => "Check your email for password reset instructions."
else
redirect_to new_password_reset_path, :notice => "Sorry, we couldn't find that email. Please try again."
end
end
def edit
#user = User.find_by_password_reset_token!(params[:id])
end
def update
#user = User.find_by_password_reset_token!(params[:id])
if #user.password_reset_sent_at < 2.hours.ago
redirect_to new_password_reset_path, :alert => "Your password reset link has expired."
elsif #user.update_attributes(params[:user])
redirect_to profile_path, :notice => "Great news: Your password has been reset."
else
render :edit
end
end
end
Looks like the problem is in your PasswordResetsController#update:
def update
#user = User.find_by_password_reset_token!(params[:id])
if #user.password_reset_sent_at < 2.hours.ago
redirect_to new_password_reset_path, :alert => "Your password reset link has expired."
elsif #user.update_attributes(params[:user])
redirect_to profile_path, :notice => "Great news: Your password has been reset."
else
render :edit
end
end
The redirect_to profile_path in particular.
If you look at the logs, you'll see this sequence:
POST "/password_resets/qlslPnuOhdyMCNseMnV3bA"
GET "/profiles/qlslPnuOhdyMCNseMnV3bA"
and the routes should be /password_resets/:id and /profiles/:id. The /password_resets wants the 'qlslPnuOhdyMCNseMnV3bA' token but /profiles wants the user's numeric ID.
Going back to the controller we see this:
redirect_to profile_path, :notice => "Great news: Your password has been reset."
You don't tell profile_path which user to use so apparently it is grabbing params[:id] to build the incorrect /profiles/qlslPnuOhdyMCNseMnV3bA URL. Try telling profile_path which user to use with something like this:
redirect_to profile_path(#user), :notice => "Great news: Your password has been reset."

factory_girl rspec test failure with before_filter

I have a newbie question here. I've been following along with the rails tutorial and I've hit a snag in my tests. When I implement the following filter...
class UsersController < ApplicationController
before_filter :authenticate, :only => [:edit, :update]
...all of my user_controller_spec.rb tests related to 'GET edit' and 'PUT update' start to fail where before they passed. Here's one that should have returned true, but returned false:
describe "GET 'edit" do
before(:each) do
#user = Factory(:user)
test_sign_in(#user)
end
it "should be successful" do
get :edit, :id => #user
response.should be_success
end
end
And this is my test_sign_in code in spec_helper.rb
def test_sign_in(user)
controller.sign_in(user)
end
The sign_in method is in SessionsHelper, which is incuded in ApplicationController:
def sign_in(user)
cookies.permanent.signed[:remember_token] = [user.id, user.salt]
current_user = user
end
I don' know how to further investigate. My guess is that since Rspec was unable to 'GET edit' there must be a log of that request that I can look at, but the test only tells me it returned false. Where can I look next?
Update: What I found in log/test.log is that every 'Processing by UsersController#index as HTML' line is followed by a redirect as follows:
Processing by UsersController#index as HTML
[1m [35mUser Load (0.2ms) [0m SELECT "users".* FROM "users" WHERE "users"."id" IS NULL LIMIT 1
Redirected to http://test.host/signin
Does this mean that every test index action is followed by a redirect to signin? If this were true it would seem consistent with tests including the test_sign_in method (above) failing and other tests like this one passing:
describe "GET 'index' for non-signed-in users" do
it "should deny access" do
get :index
response.should redirect_to(signin_path)
end
end
I'm going to try to wrap my head around the test_sign_in method again.
You need to call self in the sign_in helper.
def sign_in(user)
cookies.permanent.signed[:remember_token] = [user.id, user.salt]
self.current_user = user
end

RSpec and weird tests results

I'm trying to make a simple app. When Im testing it in browser everytyhing works just fine. Howerver, when I try to run some tests with RSpec (2.5) it fails when it comes to :create test for controller.
Here's my create method:
def create
#website = Website.new(params[:website])
if #website.save
flash[:notice] = "Website created."
redirect_to(:action => 'list')
else
render('new')
end
end
The controller test:
describe WebsitesController do
render_views
.
.
.
describe "POST 'create'" do
before(:each) do
#attr = { :adres => "www.excc.pl", :opis => "aaa "*22, :tagi => "aaa aaa aaa",
:preview => File.new(Rails.root + 'spec/fixtures/rails.png'),
:preview_mini => File.new(Rails.root + 'spec/fixtures/rails.png')}
end
describe "success" do
it "should have the right title" do
response.should have_selector("title", :content=>"Lista witryn w portfolio")
end
end
.
.
.
The result of this test:
1) WebsitesController POST 'create' should have the right title
Failure/Error: response.should have_selector("title", :content=>"Lista witryn w portfolio")
expected following output to contain a <title>Lista witryn w portfolio</title> tag:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
# ./spec/controllers/websites_controller_spec.rb:34:in `block (4 levels) in
websites_controller_spec.rb:34 refers to create method
However, this test is passed correctly (for incorrect data it should be redirected back to 'new' site with specified title):
it "should have the right title" do
post :create, :website => #attr.merge(:adres => "")
response.should have_selector("title", :content=>"Dodaj stronę WWW")
end
The second problem is...
There was a time when I've got a test result like this:
<html><body>You are being redire cted.</body></html>
... which was causing me to pull my hair out for some time until I've done sth (I don't really know what) and it was gone. Yet, it makes me scared like hell when I think that it can come back in future an ruin my happiness.
Any thoughts on this would be greatly appreciated.
It's hard to know what is being asked here, but I believe the issue is that you are not setting the conditions for success/failure. If I understand correctly, when you pass in an blank :adres attribute, the save should fail and the page should render the list action. So you want to stub the create method and return true or false depending on the expected result:
it "succeeds" do
#website = mock_model(Website,:save=>true)
Website.stub(:new) { #website }
post :create, :website => {}
# redirects
response.should have_selector("etc etc")
end
it "fails" do
#website = mock_model(Website,:save=>false)
Website.stub(:new) { #website }
post :create, :website => {}
# renders 'new'
response.should_not have_selector("etc etc")
end
Testing of the validity of the parameters should be performed in the model spec:
#website = Website.new(:adres=>"")
#website.should_not be_valid

Why is capybara/rspec losing the current_user?

The following spec is failing and failing over again. I've tried everything but can't get it to work. If I test it manually all looks fine :( Some help/tips would be really nice!
The join action should add an logged-in user to a guild/group if he/she has the right token (in the URL). If the user isn't logged in, the action redirects to the login page and saves the token and the ID to cookies. After login the user gets redirected to the join page if the cookies are set.
I've found out that the current_user get lost during the test. The session variable is still present. I've a standard Authlogic setup and all other tests are passing so I really don't know what's going wrong. I'm new to RSpec/capybara but the cucumber/capybara test (from which I'm migrating) is also failing so I think it's a capybara issue.
Failing Spec:
describe GuildsController do
fixtures :roles
def login
#user = Factory(:User)
visit login_path
fill_in 'Login', :with => #user.login
fill_in 'Password', :with => 'password'
click 'Login'
page.should have_css(".notice")
end
def assing_user_to_guild_as(role)
role_id = Role.where(:name => role).first.id
#guild.assignments << Assignment.new(:role_id => role_id, :user_id => #user.id, :guild_id => #guild.id)
end
before(:each) do
#guild = Guild.first || Factory(:Guild).build
visit root_path
end
context "a user" do
before(:each) do
login
end
it "should be able to join a guild with a valid token" do
visit "guilds/#{#guild.id}/join/#{#guild.token}"
#guild.members.include?(#user.login).should be_true
page.should have_css(".notice")
end
it "shouldn't be able to join a guild with a invalid token" do
visit "guilds/#{#guild.id}/join/#{#guild.token+"invalid"}"
#guild.members.include?(#user.login).should be_false
page.should have_css(".error")
end
end
end
Controller Action:
def join
#guild = Guild.find(params[:id])
respond_to do |format|
if current_user.nil?
flash[:error] = t("have_to_be_logged_in")
unless params[:token].nil?
cookies[:rguilds_jg_token] = params[:token]
cookies[:rguilds_jg_gid] = params[:id]
end
format.html { redirect_to(login_path) }
else
unless cookies[:rguilds_jg_token].nil? && cookies[:rguilds_jg_gid].nil?
cookies.delete(:rguilds_jg_token)
cookies.delete(:rguilds_jg_gid)
end
if #guild.verified?
if params[:token] == #guild.token
unless #guild.users.include?(current_user)
#guild.assignments << Assignment.create(:user_id => current_user.id, :role_id => Role.find_by_name("member").id)
flash[:notice] = t('guilds.joined')
format.html { redirect_to(#guild) }
else
flash[:error] = t('guilds.already_joined')
format.html { redirect_to(#guild) }
end
else
flash[:error] = t('guilds.invalid_token')
format.html { redirect_to(#guild) }
end
else
flash[:error] = t('guilds.not_verified')
format.html { redirect_to(#guild) }
end
end
end
end
"rake spec" result:
...................FF.....................................................................
Failures:
1) GuildsController a user should be able to join a guild with a valid token
Failure/Error: #guild.members.include?(#user.login).should be_true
expected false to be true
# ./spec/integration/guilds_spec.rb:72:in `block (3 levels) in <top (required)>'
2) GuildsController a user shouldn't be able to join a guild with a invalid token
Failure/Error: page.should have_css(".error")
expected #has_css?(".error") to return true, got false
# ./spec/integration/guilds_spec.rb:79:in `block (3 levels) in <top (required)>'
Finished in 7.87 seconds
90 examples, 2 failures
Gems:
gem 'rails', '3.0.0.rc'
gem "mocha"
gem "rspec-rails", ">= 2.0.0.beta.19"
gem "factory_girl_rails"
gem 'capybara'
gem "authlogic", :git => "http://github.com/odorcicd/authlogic.git", :branch => "rails3"
# In your test_helper.rb / spec_helper.rb
class ActiveRecord::Base
mattr_accessor :shared_connection
##shared_connection = nil
def self.connection
##shared_connection || retrieve_connection
end
end
# Forces all threads to share the same connection. This works on
# Capybara because it starts the web server in a thread.
ActiveRecord::Base.shared_connection = ActiveRecord::Base.connection
This is from http://gist.github.com/470808