testing errors

I have this method in user_controller_test.rb file def test_auth_bob     #check we can login     post :login, :user=> { :login => "bob", :password => "test" }     assert_session_has :user     assert_equal @bob, session[:user]     assert_response :redirect     assert_redirected_to :action=>'welcome'   end

I get this Error: test_auth_bob(UserControllerTest): NoMethodError: undefined method `assert_session_has' for #<UserControllerTest:0x 28772e0>     C:/Ruby19/lib/ruby/gems/1.9.1/gems/actionpack-2.3.5/lib/action_controller/te st_process.rb:511:in `method_missing'     test/functional/user_controller_test.rb:25:in `test_auth_bob'

A quick google search shows assert_session_has as being deprecated in 2006, so presumably it is now gone. You will have to do it a different way.

Colin

Thank you. Yes 'assert_session_has' and 'assert_session_has_no' has been replaced by 'assert_nil session'

Now I am getting another error:

Error: test_bad_signup(UserControllerTest): NoMethodError: You have a nil object when you didn't expect it! You might have expected an instance of ActiveRecord::Base. The error occurred while evaluating nil.     C:/VR/RailsPrograms/health/app/controllers/user_controller.rb:5:in `signup'     C:/Ruby19/lib/ruby/gems/1.9.1/gems/actionpack-2.3.5/lib/action_controller/ba se.rb:1331:in `perform_action'     C:/Ruby19/lib/ruby/gems/1.9.1/gems/actionpack-2.3.5/lib/action_controller/fi lters.rb:617:in `call_filters'     C:/Ruby19/lib/ruby/gems/1.9.1/gems/actionpack-2.3.5/lib/action_controller/fi lters.rb:610:in `perform_action_with_filters'     C:/Ruby19/lib/ruby/gems/1.9.1/gems/actionpack-2.3.5/lib/action_controller/be nchmarking.rb:68:in `block in perform_action_with_benchmark'     C:/Ruby19/lib/ruby/gems/1.9.1/gems/activesupport-2.3.5/lib/active_support/co re_ext/benchmark.rb:17:in `block in ms'

The 'signup' method is defined as: def signup     @user = User.new(@params[:user])     if request.post?       if @user.save         session[:user] = User.authenticate(@user.login, @user.password)         flash[:message] = "Signup successful"         redirect_to :action => "welcome"       else         flash[:warning] = "Signup unsuccessful"       end     end   end

The tester is calling 'signup' as def test_signup     #check we can signup and then login     post :signup, :user => { :login => "newbob", :password => "newpassword", :password_confirmation => "newpassword", :email => "newbob@mcbob.com" }     assert_response :redirect     assert_not_nil session[:user]     assert_session_has :user     assert_redirected_to :action=>'welcome'   end

Please tell me, what I am doing wrong?

Colin Law wrote:

I am sorry I posted wrong tester calling method. The correct calling method for testing is: def test_bad_signup     #check we can't signup without all required fields     post :signup, :user => { :login => "newbob", :password => "newpassword", :password_confirmation => "wrong" , :email => "newbob@mcbob.com"}     assert_response :success     assert_invalid_column_on_record "user", "password"     assert_template "user/signup"     assert_nil session[:user]

    post :signup, :user => { :login => "yo", :password => "newpassword", :password_confirmation => "newpassword" , :email => "newbob@mcbob.com"}     assert_response :success     assert_invalid_column_on_record "user", "login"     assert_template "user/signup"     assert_nil session[:user]

    post :signup, :user => { :login => "yo", :password => "newpassword", :password_confirmation => "wrong" , :email => "newbob@mcbob.com"}     assert_response :success     assert_invalid_column_on_record "user", ["login", "password"]     assert_template "user/signup"     assert_nil session[:user]   end

Vishwa Rao wrote:

Thank you. Yes 'assert_session_has' and 'assert_session_has_no' has been replaced by 'assert_nil session'

Now I am getting another error:

Error: test_bad_signup(UserControllerTest): NoMethodError: You have a nil object when you didn't expect it! You might have expected an instance of ActiveRecord::Base. The error occurred while evaluating nil. C:/VR/RailsPrograms/health/app/controllers/user_controller.rb:5:in `signup'

That means the error is on line 5 of user_controller (I would have expected it to be users_controller by the way, following the rails conventions). Looking at the error it seems you have some_variable[..] where some_variable is nil. You have not told us which is line 5 but if you have a look there you should see which variable it is and then be able to work out why it is nil.

If all else fails have a look at the Rails Guide on debugging and use one of the techniques there to work out what is going on. I like to use ruby-debug to break in to the failing area when the test runs and inspect the variables. In fact even if you can see the problem without debugging have a look at the guide for future reference.

Colin

1 class UserController < ApplicationController 2 before_filter :login_required, :only=> ['welcome', 'change_password', 'hidden'] 3 4 def signup 5 @user = User.new(@params[:user]) 6 if request.post? 7 if @user.save 8 session[:user] = User.authenticate(@user.login, @user.password) 9 flash[:message] = "Signup successful" 10 redirect_to :action => "welcome" 11 else 12 flash[:warning] = "Signup unsuccessful" 13 end 14 end 15 end

Here is the code listing and Line 5 is at: @user = User.new(@params[:user])

I am just instantiating User class object. How is this nil - I do not understand Rail syntax enough to find out what is going on?

Colin Law wrote:

Could you not top post please, it is easier to follow a thread if you insert your comments inline

1 class UserController < ApplicationController 2 before_filter :login_required, :only=> ['welcome', 'change_password', 'hidden'] 3 4 def signup 5 @user = User.new(@params[:user]) 6 if request.post? 7 if @user.save 8 session[:user] = User.authenticate(@user.login, @user.password) 9 flash[:message] = "Signup successful" 10 redirect_to :action => "welcome" 11 else 12 flash[:warning] = "Signup unsuccessful" 13 end 14 end 15 end

Here is the code listing and Line 5 is at: @user = User.new(@params[:user])

Read the error message again and also what I said, the error is that it is trying to evaluate something[..] where something is nil. It is not user that is nil but @params.

Did you try my suggestion of debugging if you could not work it out?

So why is @params nil? Probably because it should be params not @params.

Colin

Colin Law wrote:

Could you not top post please, it is easier to follow a thread if you insert your comments inline

9 flash[:message] = "Signup successful" 10 redirect_to :action => "welcome" 11 else 12 flash[:warning] = "Signup unsuccessful" 13 end 14 end 15 end

Here is the code listing and Line 5 is at: @user = User.new(@params[:user])

Read the error message again and also what I said, the error is that it is trying to evaluate something[..] where something is nil. It is not user that is nil but @params.

Did you try my suggestion of debugging if you could not work it out?

So why is @params nil? Probably because it should be params not @params.

Colin

I got another error on the line assert_invalid_column_on_record "user", "login"

So I googled and found that it is deprecated and hence suggested using: assert(find_record_in_template("user").errors.invalid?(:password))

When I use this I get error : test_bad_signup(UserControllerTest): NoMethodError: undefined method `find_record_in_template' for #<UserControllerTe st:0x29fbe40>     C:/Ruby19/lib/ruby/gems/1.9.1/gems/actionpack-2.3.5/lib/action_controller/te st_process.rb:511:in `method_missing'     test/functional/user_controller_test.rb:45:in `test_bad_signup'

I cannot find out on Google about error on 'find_record_in_template'. I don't know how to fix this - is this also deprecated or is it my syntax error?

Also you mentioned about debugging - is there a simple document that discribes this?

Colin Law wrote: ... I got another error on the line assert_invalid_column_on_record "user", "login"

So I googled and found that it is deprecated and hence suggested using: assert(find_record_in_template("user").errors.invalid?(:password))

When I use this I get error : test_bad_signup(UserControllerTest): NoMethodError: undefined method `find_record_in_template' for #<UserControllerTe st:0x29fbe40> C:/Ruby19/lib/ruby/gems/1.9.1/gems/actionpack-2.3.5/lib/action_controller/te st_process.rb:511:in `method_missing' test/functional/user_controller_test.rb:45:in `test_bad_signup'

I cannot find out on Google about error on 'find_record_in_template'. I don't know how to fix this - is this also deprecated or is it my syntax error?

http://dev.rubyonrails.org/ticket/7385 suggests this has also gone, you will have to find another way.

Also you mentioned about debugging - is there a simple document that discribes this?

I think I suggested the Rails Guide on debugging (google rails guides). If you have not already seen them the Getting Started guide and ActiveRecord Associations are compulsory reading.

Colin

Colin Law wrote:

I cannot find out on Google about error on 'find_record_in_template'. I don't know how to fix this - is this also deprecated or is it my syntax error?

http://dev.rubyonrails.org/ticket/7385 suggests this has also gone, you will have to find another way.

I used u = User.new assert !u.valid? assert u.errors.on(:password) That worked.

I think I suggested the Rails Guide on debugging (google rails guides). If you have not already seen them the Getting Started guide and ActiveRecord Associations are compulsory reading.

Colin

Do I have to use Mongrel Server to debug? Can I use IIS? Are there advantages/diadvantages. Also is there a light weight open source code/editor for Ruby. Right now I am using Notepad++.

Thank your for your patience and guidance. I am learning a lot. Coming from .NET world it is kind of hard to get the syntax. I read ruby guide. it is difficult in one reading. May have to go back and forth between writing code and reading.

Also when I use this test code: post :login, :user=> { :login => "bob", :password => "test" }     assert_nil session[:user] # assert_session_has & _has_no have been deprecated     assert_equal @bob, session[:user]

I get this failure - what does it mean?   1) Failure: test_auth_bob(UserControllerTest) [test/functional/user_controller_test.rb:25]: Expected #<User id: 1000001, login: "bob", hashed_password: "77a0d943cdbace52716 a9ef9fae12e45e2788d39", email: "vihrao@gmail.com", salt: "1000", created_at: "20 10-01-27 21:33:33", updated_at: "2010-01-27 21:33:33", first_name: nil, last_nam e: nil, phone_num: nil, user_role: "patient"> to be nil.

Thank you again.

Colin Law wrote:

I cannot find out on Google about error on 'find_record_in_template'. I don't know how to fix this - is this also deprecated or is it my syntax error?

http://dev.rubyonrails.org/ticket/7385 suggests this has also gone, you will have to find another way.

I used u = User.new assert !u.valid? assert u.errors.on(:password) That worked.

I think I suggested the Rails Guide on debugging (google rails guides). If you have not already seen them the Getting Started guide and ActiveRecord Associations are compulsory reading.

Colin

Do I have to use Mongrel Server to debug? Can I use IIS? Are there advantages/diadvantages.

I doubt if you can use IIS for debugging. I don't know about it. If you are serious about Rails I would suggest having a look at Ubuntu (a flavour of Linux). Linux manages Rails much better than windows. You can set your PC up to dual boot windows and Ubuntu so you can still keep windows also. I did this and now very rarely use windows. It is great. Have a look at the free downloadable guide at Main page - Ubuntu Pocket Guide and Reference if you are interested. Of course you would have to put your rails stuff on hold for a while in order to get the hang of Ubuntu.

Also is there a light weight open source code/editor for Ruby. Right now I am using Notepad++

I use jEdit, which is open source and runs on win and linux (and mac).

Thank your for your patience and guidance. I am learning a lot. Coming from .NET world it is kind of hard to get the syntax. I read ruby guide. it is difficult in one reading. May have to go back and forth between writing code and reading.

I would also suggest a new tutorial which is not yet complete (but goes far enough to get you going) at http://www.railstutorial.org/

Also when I use this test code: post :login, :user=> { :login => "bob", :password => "test" } assert_nil session[:user] # assert_session_has & _has_no have been deprecated assert_equal @bob, session[:user]

I get this failure - what does it mean? 1) Failure: test_auth_bob(UserControllerTest) [test/functional/user_controller_test.rb:25]: Expected #<User id: 1000001, login: "bob", hashed_password: "77a0d943cdbace52716 a9ef9fae12e45e2788d39", email: "vihrao@gmail.com", salt: "1000", created_at: "20 10-01-27 21:33:33", updated_at: "2010-01-27 21:33:33", first_name: nil, last_nam e: nil, phone_num: nil, user_role: "patient"> to be nil.

Again if you look carefully at what appears at first to be random words it says At user_controller_test.rb line 25, expected ........ to be nil.

Colin Law wrote:    Have a look at the free downloadable guide at

Main page - Ubuntu Pocket Guide and Reference if you are interested. Of course you would have to put your rails stuff on hold for a while in order to get the hang of Ubuntu.

I am going to checkout ubuntu linux. it should be easy as I worked with unix earlier. I will setup then debugger in it. may be isntall VMware on windows box and install ubuntu on it.

I use jEdit, which is open source and runs on win and linux (and mac).

Thanks, I will try this.

assert_nil session[:user] so it is saying that it expected it to be nil, but it is not, and is showing you what it is.

I got this error figured out.

Now all assert save fails. I hope this is the last error that I will have to overcome.

Failure: test_send_new_password(UserTest) [test/unit/user_test.rb:145]: Failed assertion, no message given.

user_test_controller.rb code line 145 refers to assert u.save as below:

def test_send_new_password u = User.new :login => "bob",     :password => "test",     :email => "bob@mcbob.com",     :phone_num => "(212)111-1111",     :first_name=> "robert",     :last_name => "baker",     :user_role => "Patient"

    assert u.save end

The code being called in user.rb: def send_new_password     new_pass = User.random_string(10)     self.password = self.password_confirmation = new_pass     self.save     Notifications.deliver_forgot_password(self.email, self.login, new_pass)   end

The column names in the database are all correct as below: id login hashed_password email salt created_at updated-at first_name last_name phone_num user_role

Why is ssert save failing - it does not give me any errors?

Colin Law wrote: Have a look at the free downloadable guide at

Main page - Ubuntu Pocket Guide and Reference if you are interested. Of course you would have to put your rails stuff on hold for a while in order to get the hang of Ubuntu.

I am going to checkout ubuntu linux. it should be easy as I worked with unix earlier. I will setup then debugger in it. may be isntall VMware on windows box and install ubuntu on it.

I prefer a dual boot personally but it is a matter of preference. Many install ubuntu then run windows in VMWare or VirtualBox.

Before you install rails, google for install rails ubuntu as you want to install some bits from ubuntu repository and some using gem install. I have a script I use to install it, email me privately when you get there if you want it.

I use jEdit, which is open source and runs on win and linux (and mac).

Thanks, I will try this.

assert_nil session[:user] so it is saying that it expected it to be nil, but it is not, and is showing you what it is.

I got this error figured out.

Now all assert save fails. I hope this is the last error that I will have to overcome.

Failure: test_send_new_password(UserTest) [test/unit/user_test.rb:145]: Failed assertion, no message given.

user_test_controller.rb code line 145 refers to assert u.save as below:

def test_send_new_password u = User.new :login => "bob", :password => "test", :email => "bob@mcbob.com", :phone_num => "(212)111-1111", :first_name=> "robert", :last_name => "baker", :user_role => "Patient"

assert u.save end

The code being called in user.rb: def send_new_password new_pass = User.random_string(10) self.password = self.password_confirmation = new_pass self.save Notifications.deliver_forgot_password(self.email, self.login, new_pass) end

The column names in the database are all correct as below: id login hashed_password email salt created_at updated-at first_name last_name phone_num user_role

Why is ssert save failing - it does not give me any errors?

Perhaps you have a validation failing. If you put assert u.valid?, u.errors.full_messages before the save valid? will fail and display the validation errors. Or you could probably just put the messages on the assert u.save. I like to do the valid test explicitly though.

Colin

Colin Law wrote:

Colin Law wrote:

I prefer a dual boot personally but it is a matter of preference. Many install ubuntu then run windows in VMWare or VirtualBox.

Going to check my pc If I can install second drive where I can put vmware and linux on it. I prefer linux on vmware on windows. The other way you suggested windows on vmware on linux - any specific advantages?

Before you install rails, google for install rails ubuntu as you want to install some bits from ubuntu repository and some using gem install. I have a script I use to install it, email me privately when you get there if you want it.

How do I send personal email to you from the forum. I tried to search your name under users list. I cannot and it has some 671 pages of users listed that cannot be sorted either. I see my name has link in the posting, may be you can send a message with your email to me. If not please tell me how I can send message to you. I defnitely want to use Linux and learn all the tricks I can with Ruby on Linux - that is the reason I am switching from .NET to Ruby so that the app can run on multiple OS.

Perhaps you have a validation failing. If you put assert u.valid?, u.errors.full_messages

Thank you. This is awesome - gives me specific list of validation failures

Colin Law wrote:

Colin Law wrote:

I prefer a dual boot personally but it is a matter of preference. Many install ubuntu then run windows in VMWare or VirtualBox.

Going to check my pc If I can install second drive where I can put vmware and linux on it. I prefer linux on vmware on windows. The other way you suggested windows on vmware on linux - any specific advantages?

I don't know the pros and cons of the two virtual machine layouts, as I said I prefer a dual boot. The ubuntu install can split your disk into two partitions and install itself on one, leaving windows in the other after squashing it down to fit. Then at boot time you choose which you want. We are getting a bit OT here, if you are interested in ubuntu I would suggest subscribing to the ubuntu users list https://lists.ubuntu.com/mailman/listinfo/ubuntu-users

Before you install rails, google for install rails ubuntu as you want to install some bits from ubuntu repository and some using gem install. I have a script I use to install it, email me privately when you get there if you want it.

How do I send personal email to you from the forum. I tried to search your name under users list. I cannot and it has some 671 pages of users listed that cannot be sorted either. I see my name has link in the posting, may be you can send a message with your email to me. If not please tell me how I can send message to you. I defnitely want to use Linux and learn all the tricks I can with Ruby on Linux - that is the reason I am switching from .NET to Ruby so that the app can run on multiple OS.

As I use the list as a mailing list you should be able to see my email address, but perhaps you cannot on the forum web interface. I find it works much better as a mailing list than the forum. But again that is a matter of taste. I can't see yours so I presume you are using it as a forum. Mine is clanlaw at googlemail dot com

Perhaps you have a validation failing. If you put assert u.valid?, u.errors.full_messages

Thank you. This is awesome - gives me specific list of validation failures

Glad to be of help

Colin