ruby on rails - Rspec be_valid fails on a valid test -



ruby on rails - Rspec be_valid fails on a valid test -

i'm learning tdd rails 4 , rspec. i've made test cases user model check password lengths. have 2 tests far checks whether user input password short , 1 password between 6 - 10 characters.

so far, "password short" test passes:

it "validation says password short if password less 6 characters" short_password = user.create(email: "tester@gmail.com", password: "12345") expect(short_password).not_to be_valid end

however, on test have valid password, fails:

it "validation allows passwords larger 6 , less 10" good_password = user.create(email: "tester2@gmail.com", password: "blahblah") expect(good_password).to be_valid end

and error:

failure/error: expect(good_password).to be_valid expected #<user id: 1, email: "tester2@gmail.com", created_at: "2014-06-21 02:43:42", updated_at: "2014-06-21 02:43:42", password_digest: nil, password: nil, password_hash: "$2a$10$7u0xddecc6kjcai32lbw7uzv9n7xybfohzwdconu5cdm...", password_salt: "$2a$10$7u0xddecc6kjcai32lbw7u"> valid, got errors: password can't blank, password short (minimum 6 characters) # ./spec/models/user_spec.rb:12:in `block (3 levels) in <top (required)>'

edit: here's model code:

class user < activerecord::base has_many :pets, dependent: :destroy accepts_nested_attributes_for :pets, :allow_destroy => true valid_email_regex = /\a[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i validates :email, presence: true, format: { with: valid_email_regex }, uniqueness: true validates :password, presence: true, :length => 6..10, :confirmation => true #callbacks before_save :encrypt_password after_save :clear_password #method authenticate user , password def self.authenticate(email, password) user = find_by_email(email) if user && user.password_hash == bcrypt::engine.hash_secret(password, user.password_salt) user else nil end end #method encrypt password def encrypt_password if password.present? self.password_salt = bcrypt::engine.generate_salt self.password_hash = bcrypt::engine.hash_secret(password, password_salt) end end #clears password def clear_password self.password = nil end end

i'm confused on why password nil when create test object.

thanks!

the problem password field not mass assignable. why password nil in output message.

try instead:

it "validation allows passwords larger 6 , less 10" good_password = user.create(email: "tester2@gmail.com") good_password.password = "blahblah" expect(good_password).to be_valid end

note first test passing accidentally - has same problem sec test (password isn't beingness assigned). means aren't testing password rejected when less 6 characters atm.

see this article on mass assignment more details.

edit: leo correa's comment may suggest may not case you. posting model code help...

ruby-on-rails rspec

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -