为什么我不能在 rspec 中创建对某些内容有意义的活动记录? [英] Why can't I create active records that count for something in rspec?

查看:28
本文介绍了为什么我不能在 rspec 中创建对某些内容有意义的活动记录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么我创建记录后计数为零?

Why after I create records is the count zero ?

require 'spec_helper'
...
describe "keep admin" do

  its "Can't delete the only admin" do
    user1 = User.create(username: 'user1', password: 'abc123', admin: true)
    user2 = User.create(username: 'user2', password: 'def456', admin: true)
    user3 = User.create(username: 'user3', password: 'ghi798', admin: true)
    #User.delete_me(user1) currently commented out to be sure.
    #User.delete_me(user2)
    #User.delete_me(user3)
    expect(User.where(admin: true).count).to eq 3
  end

end

  1) keep admin Can't delete the only admin should eq 3
     Failure/Error: expect(User.where(admin: true).count).to eq 3

       expected: 3
            got: 0

       (compared using ==)
     # ./spec/models/user_spec.rb:24:in `block (2 levels) in <top (required)>'

Finished in 0.41264 seconds
5 examples, 1 failure

$ RAILS_ENV=test rails c
User
Loading test environment (Rails 3.2.17)
2.0.0-p247 :001 > User
 => User(id: integer, username: string, pwd_hashed: string, salt: string,  
created_at: datetime, updated_at: datetime, admin: boolean) 

我可以在使用应用程序本身时正常创建用户.我尝试了 createcreate!.为什么计数总是零?

I can create users ok when using the app itself. I tried create and create!. Why is the count always zero ?

编辑 - 完整的用户模型 -

Edit - full User model -

class User < ActiveRecord::Base
require 'digest/sha1'
  attr_accessor :password_confirmation
  attr_accessor :admin

  validates_presence_of     :username
  validates_uniqueness_of   :username
  validates_confirmation_of :password
  validate :password_non_blank

  def self.delete_me(user)
    how_many_admins = User.where(admin: true).count
    if how_many_admins > 1 
      puts "delete ok!"
      user.delete
    else
      puts "delete not ok!"
    end 
  end 

  def self.authenticate(name, password)
    user = self.find_by_username(name)
    if user
      expected_password = encrypted_password(password, user.salt)
      if user.pwd_hashed != expected_password
        user = nil 
      end 
    end 
    user
  end 
  def password
    @password
  end 

  def password=(pwd)
    @password = pwd 
    return if pwd.blank?
    create_new_salt
    self.pwd_hashed = User.encrypted_password(self.password, self.salt)
  end 

  def is_admin
    admin ? 'Yes' : 'No'
  end 

private
  def password_non_blank
    errors.add(:password, "Missing password") if pwd_hashed.blank?
  end 

  def create_new_salt
    self.salt = self.object_id.to_s + rand.to_s
  end 

  def self.encrypted_password(password, salt)
    string_to_hash = password + "wibble" + salt
    Digest::SHA1.hexdigest(string_to_hash)
  end 

end

推荐答案

你的 admin 属性没有被持久化到数据库中(因为调用了 attr_accessor),所以在 admin 为 true 将始终返回 0.

Your admin attribute isn't being persisted to the database (because of the call to attr_accessor), so counting users where admin is true will always return 0.

您的密码确认似乎也应该失败,因为您实际上并未提供确认.

It also looks like your password confirmation should fail, since you're not actually providing a confirmation.

这篇关于为什么我不能在 rspec 中创建对某些内容有意义的活动记录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆