NoMethodError in Unit Test - Rails Newcomer

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 = "[email protected]"

ok

c.password = c.password_confirmation = "magicwand"
assert c.save

The error I keep getting is this (it happens in all of my tests):

  1. 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:inmethod_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?

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= …

Chris C. 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:inmethod_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:invalidates_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:invalidates_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:inrun_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:inrun_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:invalid?’
    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:insave’
    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:intransaction’
    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:insave’
    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:inencrypt_password’
C:/www/satPlace/config/…/app/models/customer.rb:23:in password=' test/unit/customer_test.rb:28:intest_passwordchange’

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs