我可以在控制台中创建用户:
2.0.0-p247 :020 > User.create(username: 'user30', password: 'abc123', admin: true)
(0.3ms) BEGIN
User Exists (0.6ms) SELECT 1 AS one FROM `users` WHERE `users`.`username` = BINARY 'user30' LIMIT 1
SQL (0.5ms) INSERT INTO `users` (`admin`, `created_at`, `pwd_hashed`, `salt`, `updated_at`, `username`) VALUES (NULL, '2014-07-17 01:34:41', 'd3272da7e3fc0f6e1b035dfb10e6f412ae84fac8', '415066400.06787292634781616', '2014-07-17 01:34:41', 'user30')
(75.6ms) COMMIT
=> #<User id: 14, username: "user30", pwd_hashed: "d3272da7e3fc0f6e1b035dfb10e6f412ae84fac8", salt: "415066400.06787292634781616", created_at: "2014-07-17 01:34:41", updated_at: "2014-07-17 01:34:41", admin: nil>
2.0.0-p247 :021 > User.create(username: 'user31', password: 'abc123', admin: true)
(0.3ms) BEGIN
User Exists (1.4ms) SELECT 1 AS one FROM `users` WHERE `users`.`username` = BINARY 'user31' LIMIT 1
SQL (0.4ms) INSERT INTO `users` (`admin`, `created_at`, `pwd_hashed`, `salt`, `updated_at`, `username`) VALUES (NULL, '2014-07-17 01:34:46', '1ea5630a3c28b36e714625c4b44f3f951ef16c7a', '416177200.3102826047096757', '2014-07-17 01:34:46', 'user31')
(77.5ms) COMMIT
=> #<User id: 15, username: "user31", pwd_hashed: "1ea5630a3c28b36e714625c4b44f3f951ef16c7a", salt: "416177200.3102826047096757", created_at: "2014-07-17 01:34:46", updated_at: "2014-07-17 01:34:46", admin: nil>
2.0.0-p247 :022 > User.create(username: 'user32', password: 'abc123', admin: true)
(0.3ms) BEGIN
User Exists (0.6ms) SELECT 1 AS one FROM `users` WHERE `users`.`username` = BINARY 'user32' LIMIT 1
SQL (0.6ms) INSERT INTO `users` (`admin`, `created_at`, `pwd_hashed`, `salt`, `updated_at`, `username`) VALUES (NULL, '2014-07-17 01:34:50', '9aeb6cfcb80f57a08c421999dcfb5ad332317789', '417559200.14647833904112617', '2014-07-17 01:34:50', 'user32')
(12.0ms) COMMIT
=> #<User id: 16, username: "user32", pwd_hashed: "9aeb6cfcb80f57a08c421999dcfb5ad332317789", salt: "417559200.14647833904112617", created_at: "2014-07-17 01:34:50", updated_at: "2014-07-17 01:34:50", admin: nil>
2.0.0-p247 :023 >
2.0.0-p247 :005 > User.count
(0.6ms) SELECT COUNT(*) FROM `users`
=> 3
但是当我有
时的rspecdescribe "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)
expect(User.where(admin: true).count).to eq 3
end
end
为什么我得到0而不是3:
Failures:
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.37602 seconds
5 examples, 1 failure
用户模型:
$ cat app/models/user.rb
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
答案 0 :(得分:1)
您的创建调用中的管理员分配似乎无效。在console
您在所有用户中设置admin: true
,然后查看日志:
User.create(username: 'user30', password: 'abc123', admin: true)
#<User id: 14, username: "user30", pwd_hashed: "d3272da7e3fc0f6e1b035dfb10e6f412ae84fac8", salt: "415066400.06787292634781616", created_at: "2014-07-17 01:34:41", updated_at: "2014-07-17 01:34:41", admin: nil>
admin: nil
是存储的内容。
您正在尝试将ruby布尔值设置为我猜测的字符串&#34;&#34;数据库中的字段?如果是这样,试试这个:
User.create(username: 'user30', password: 'abc123', admin: "true")
然后将您的rspec测试更改为:
expect(User.where(admin: "true").count).to eq(3)