I''m working on user authentication system to get familiar with rails, and have basically solved most of my apps problems till now. I keep getting the NoMethodError, and can''t understand why. Model: customer.rb class Customer < ActiveRecord::Base ... def self.password=(pass) @password=pass salt = Customer.random_string(10) if !salt? self.hashed_password = Customer.encrypt_password(@password, salt) end end Test: customer_test.rb require File.dirname(__FILE__) + ''/../test_helper'' class CustomerTest < Test::Unit::TestCase self.use_instantiated_fixtures = true fixtures :customers ... # change password @longbob.password = @longbob.password_confirmation = "newlongpassword" assert @longbob.save c = Customer.new c.email_address = "jimbo@mcjim.com" # ok c.password = c.password_confirmation = "magicwand" assert c.save The error I keep getting is this (it happens in all of my tests): 4) Error: test_passwordchange(CustomerTest): NoMethodError: undefined method `password='' for #<Customer:0x343de40> C:/Program Files/Ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.3/lib/active_record/base.rb:1789:in `method_missing'' test/unit/customer_test.rb:28:in `test_passwordchange'' I''m not sure what I''m doing wrong, but I''m 100% sure that the password method exists for that model. Any ideas? -- Posted via http://www.ruby-forum.com/.
Chris Carter
2006-Jul-02 04:23 UTC
[Rails] Re: NoMethodError in Unit Test - Rails Newcomer
Trevor Hunsaker wrote:> def self.password=(pass) > @password=pass > salt = Customer.random_string(10) if !salt? > self.hashed_password = Customer.encrypt_password(@password, salt) > end > endIt should be just def password= ... -- Posted via http://www.ruby-forum.com/.
Trevor Hunsaker
2006-Jul-02 04:45 UTC
[Rails] Re: NoMethodError in Unit Test - Rails Newcomer
Chris Carter wrote:> Trevor Hunsaker wrote: > >> def self.password=(pass) >> @password=pass >> salt = Customer.random_string(10) if !salt? >> self.hashed_password = Customer.encrypt_password(@password, salt) >> end >> end > > It should be just def password= ...Omitting the ''self,'' I get this more-verbose error for some of the tests: 1) Error: test_bad_logins(CustomerTest): NoMethodError: undefined method `password'' for #<Customer:0x342d0a0> C:/Program Files/Ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.3/lib/active_record/base.rb:1792:in `method_missing'' C:/Program Files/Ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.3/lib/active_record/validations.rb:300:in `send'' C:/Program Files/Ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.3/lib/active_record/validations.rb:300:in `validates_each'' C:/Program Files/Ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.3/lib/active_record/validations.rb:299:in `each'' C:/Program Files/Ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.3/lib/active_record/validations.rb:299:in `validates_each'' C:/Program Files/Ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.3/lib/active_record/validations.rb:296:in `call'' C:/Program Files/Ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.3/lib/active_record/validations.rb:794:in `run_validations'' C:/Program Files/Ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.3/lib/active_record/validations.rb:788:in `each'' C:/Program Files/Ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.3/lib/active_record/validations.rb:788:in `run_validations'' C:/Program Files/Ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.3/lib/active_record/validations.rb:752:in `valid_without_callbacks'' C:/Program Files/Ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.3/lib/active_record/callbacks.rb:306:in `valid?'' C:/Program Files/Ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.3/lib/active_record/validations.rb:723:in `save_without_transactions'' C:/Program Files/Ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.3/lib/active_record/transactions.rb:126:in `save'' C:/Program Files/Ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.3/lib/active_record/transactions.rb:126:in `transaction'' C:/Program Files/Ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.3/lib/active_record/transactions.rb:91:in `transaction'' C:/Program Files/Ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.3/lib/active_record/transactions.rb:118:in `transaction'' C:/Program Files/Ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.3/lib/active_record/transactions.rb:126:in `save'' test/unit/customer_test.rb:70:in `test_bad_logins'' and this one for some of the others: 4) Error: test_passwordchange(CustomerTest): TypeError: cannot convert nil into String C:/www/satPlace/config/../app/models/customer.rb:42:in `+'' C:/www/satPlace/config/../app/models/customer.rb:42:in `encrypt_password'' C:/www/satPlace/config/../app/models/customer.rb:23:in `password='' test/unit/customer_test.rb:28:in `test_passwordchange'' -- Posted via http://www.ruby-forum.com/.