我有一个设计认证的客户模型:
class Customer < ActiveRecord::Base
devise :database_authenticatable, :registerable, :omniauthable,
:recoverable, :rememberable, :trackable
validates_presence_of :name
validates_uniqueness_of :nickname
has_many :messages
has_many :orders
acts_as_voter
before_save :complete_nickname
def complete_nickname
if !self.nickname?
self.nickname = self.email
end
end
end
我正在测试rspec,所以在spec / factories文件夹中我有customers.rb:
require 'ffaker'
FactoryGirl.define do
factory :customer do
name { FFaker::Name.first_name }
surname { FFaker::Name.last_name }
nickname { FFaker::Internet.user_name }
email { FFaker::Internet.email }
password 'antoniorossi'
password_confirmation 'antoniorossi'
end
end
并在spec / models customer_spec.rb
中 require 'rails_helper'
require 'ffaker'
RSpec.describe Customer, :type => :model do
it 'has a valid factory' do
expect(build(:customer)).to be_valid
end
it 'has a valid factory with blank field' do
expect(build(:customer, surname: nil)).to be_valid
expect(build(:customer, nickname: nil)).to be_valid
end
before do
FactoryGirl.create(:customer, email: 'taken_mail')
FactoryGirl.create(:customer, nickname: 'taken_nickname')
end
it 'it is invalidy without name' do
expect(FactoryGirl.build(:customer, name: nil)).to_not be_valid
end
it 'it is invalidy without email' do
expect(FactoryGirl.build(:customer, email: nil)).to_not be_valid
end
it 'it is invalidy without password confirmation' do
expect(FactoryGirl.build(:customer, password_confirmation: nil)).to_not be_valid
end
it 'it is invalidy if password and password_confirmation do not match' do
expect(FactoryGirl.build(:customer, password_confirmation: 'antonioross')).to_not be_valid
end
it "it is invalidy with an already used email" do
expect(FactoryGirl.build(:customer, email: 'taken_mail')).to_not be_valid
end
it "it is invalidy with an already used nickname" do
expect(FactoryGirl.build(:customer, nickname: 'taken_nickname')).to_not be_valid
end
end
执行rspec命令我有输出:
Customer
(1.1ms) BEGIN
(2.2ms) COMMIT
(1.2ms) BEGIN
(0.4ms) SAVEPOINT active_record_1
Customer Exists (1.3ms) SELECT 1 AS one FROM "customers" WHERE "customers"."nickname" = 'ricky_grimes' LIMIT 1
SQL (1.0ms) INSERT INTO "customers" ("name", "surname", "nickname", "email", "encrypted_password", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING "id" [["name", "Erika"], ["surname", "Wehner"], ["nickname", "ricky_grimes"], ["email", "taken_mail"], ["encrypted_password", "$2a$04$I69kG7ajhgVy9u4pZdR1xuTrvmjOfW2sX74Y0jJH3gFu9pUKejCli"], ["created_at", "2015-11-27 15:45:18.830500"], ["updated_at", "2015-11-27 15:45:18.830500"]]
(0.3ms) RELEASE SAVEPOINT active_record_1
(0.3ms) SAVEPOINT active_record_1
Customer Exists (0.8ms) SELECT 1 AS one FROM "customers" WHERE "customers"."nickname" = 'taken_nickname' LIMIT 1
SQL (0.7ms) INSERT INTO "customers" ("name", "surname", "nickname", "email", "encrypted_password", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING "id" [["name", "Britney"], ["surname", "Conn"], ["nickname", "taken_nickname"], ["email", "savion_rogahn@conroy.ca"], ["encrypted_password", "$2a$04$ZuTw5k2PVQZ2T0z9TrS3G.s7NKGd1uX88gGms96Y7xn03vpu6Ksi2"], ["created_at", "2015-11-27 15:45:18.847077"], ["updated_at", "2015-11-27 15:45:18.847077"]]
(0.4ms) RELEASE SAVEPOINT active_record_1
Customer Exists (0.6ms) SELECT 1 AS one FROM "customers" WHERE "customers"."nickname" = 'georgianna.dickens' LIMIT 1
(0.4ms) ROLLBACK
has a valid factory
(0.3ms) BEGIN
(0.2ms) COMMIT
(0.2ms) BEGIN
(0.3ms) SAVEPOINT active_record_1
Customer Exists (0.8ms) SELECT 1 AS one FROM "customers" WHERE "customers"."nickname" = 'davonte_johnston' LIMIT 1
SQL (1.6ms) INSERT INTO "customers" ("name", "surname", "nickname", "email", "encrypted_password", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING "id" [["name", "Johan"], ["surname", "Bashirian"], ["nickname", "davonte_johnston"], ["email", "taken_mail"], ["encrypted_password", "$2a$04$UMEYJWN2mOYTIUoUqkl4TuQtz3GnOwZBPoZDQ3nXXCEJpyfRhIRMO"], ["created_at", "2015-11-27 15:45:18.876060"], ["updated_at", "2015-11-27 15:45:18.876060"]]
(0.4ms) RELEASE SAVEPOINT active_record_1
(0.3ms) SAVEPOINT active_record_1
Customer Exists (0.7ms) SELECT 1 AS one FROM "customers" WHERE "customers"."nickname" = 'taken_nickname' LIMIT 1
SQL (0.6ms) INSERT INTO "customers" ("name", "surname", "nickname", "email", "encrypted_password", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING "id" [["name", "Nona"], ["surname", "Kozey"], ["nickname", "taken_nickname"], ["email", "marcel@balistreriflatley.com"], ["encrypted_password", "$2a$04$G9epfoLwQ16QRyx2G5UHC.6P724N9VlGluZLiyn8sS0UYvzb8atjS"], ["created_at", "2015-11-27 15:45:18.892194"], ["updated_at", "2015-11-27 15:45:18.892194"]]
(0.3ms) RELEASE SAVEPOINT active_record_1
Customer Exists (0.6ms) SELECT 1 AS one FROM "customers" WHERE "customers"."nickname" = 'clara_upton' LIMIT 1
Customer Exists (0.8ms) SELECT 1 AS one FROM "customers" WHERE "customers"."nickname" IS NULL LIMIT 1
(0.7ms) ROLLBACK
has a valid factory with blank field
(0.3ms) BEGIN
(0.2ms) COMMIT
(0.2ms) BEGIN
(0.3ms) SAVEPOINT active_record_1
Customer Exists (0.6ms) SELECT 1 AS one FROM "customers" WHERE "customers"."nickname" = 'della.bechtelar' LIMIT 1
SQL (0.6ms) INSERT INTO "customers" ("name", "surname", "nickname", "email", "encrypted_password", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING "id" [["name", "Jazmyn"], ["surname", "Keeling"], ["nickname", "della.bechtelar"], ["email", "taken_mail"], ["encrypted_password", "$2a$04$WFKmWHoJY7ao17HxexhsXuXQMavFxegG2RNZz2bh41UmTo1MNvI/m"], ["created_at", "2015-11-27 15:45:18.926027"], ["updated_at", "2015-11-27 15:45:18.926027"]]
(0.3ms) RELEASE SAVEPOINT active_record_1
(0.3ms) SAVEPOINT active_record_1
Customer Exists (0.6ms) SELECT 1 AS one FROM "customers" WHERE "customers"."nickname" = 'taken_nickname' LIMIT 1
SQL (0.7ms) INSERT INTO "customers" ("name", "surname", "nickname", "email", "encrypted_password", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING "id" [["name", "Sean"], ["surname", "Casper"], ["nickname", "taken_nickname"], ["email", "ozella.schroeder@von.com"], ["encrypted_password", "$2a$04$f7.az4xueyZxHOMGKNu6ReQJ9sCykNbk2a5/3cb8mwrbxNtf/eSq6"], ["created_at", "2015-11-27 15:45:18.939113"], ["updated_at", "2015-11-27 15:45:18.939113"]]
(0.4ms) RELEASE SAVEPOINT active_record_1
Customer Exists (0.6ms) SELECT 1 AS one FROM "customers" WHERE "customers"."nickname" = 'neil_prosacco' LIMIT 1
(0.3ms) ROLLBACK
it is invalidy without name
(0.2ms) BEGIN
(0.2ms) COMMIT
(0.2ms) BEGIN
(0.3ms) SAVEPOINT active_record_1
Customer Exists (0.7ms) SELECT 1 AS one FROM "customers" WHERE "customers"."nickname" = 'kailyn_carroll' LIMIT 1
SQL (0.6ms) INSERT INTO "customers" ("name", "surname", "nickname", "email", "encrypted_password", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING "id" [["name", "Chet"], ["surname", "Heidenreich"], ["nickname", "kailyn_carroll"], ["email", "taken_mail"], ["encrypted_password", "$2a$04$OLVf3jnY.2sjGtMa3qjBOOHRW2WWMqfnNGTLYJvCJVW36oHMJxSjK"], ["created_at", "2015-11-27 15:45:19.009064"], ["updated_at", "2015-11-27 15:45:19.009064"]]
(0.3ms) RELEASE SAVEPOINT active_record_1
(0.3ms) SAVEPOINT active_record_1
Customer Exists (0.7ms) SELECT 1 AS one FROM "customers" WHERE "customers"."nickname" = 'taken_nickname' LIMIT 1
SQL (0.5ms) INSERT INTO "customers" ("name", "surname", "nickname", "email", "encrypted_password", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING "id" [["name", "Rhea"], ["surname", "Swift"], ["nickname", "taken_nickname"], ["email", "brandyn@olson.ca"], ["encrypted_password", "$2a$04$TEtF/1Y2OL4zw5h.9coJBupMQaRqTESiuLThVY9sGT91oRkLEd1W."], ["created_at", "2015-11-27 15:45:19.022284"], ["updated_at", "2015-11-27 15:45:19.022284"]]
(0.3ms) RELEASE SAVEPOINT active_record_1
Customer Exists (0.8ms) SELECT 1 AS one FROM "customers" WHERE "customers"."nickname" = 'laurine_boyer' LIMIT 1
(0.5ms) ROLLBACK
it is invalidy without email (FAILED - 1)
(0.3ms) BEGIN
(0.2ms) COMMIT
(0.2ms) BEGIN
(0.3ms) SAVEPOINT active_record_1
Customer Exists (0.6ms) SELECT 1 AS one FROM "customers" WHERE "customers"."nickname" = 'kali' LIMIT 1
SQL (0.5ms) INSERT INTO "customers" ("name", "surname", "nickname", "email", "encrypted_password", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING "id" [["name", "Bennett"], ["surname", "Stamm"], ["nickname", "kali"], ["email", "taken_mail"], ["encrypted_password", "$2a$04$fG2XjrqKQC.ABOpqnhfsFuLWTgYbdq5I.n9taI8i.70IQOG3gWx5G"], ["created_at", "2015-11-27 15:45:19.050392"], ["updated_at", "2015-11-27 15:45:19.050392"]]
(0.3ms) RELEASE SAVEPOINT active_record_1
(0.3ms) SAVEPOINT active_record_1
Customer Exists (0.7ms) SELECT 1 AS one FROM "customers" WHERE "customers"."nickname" = 'taken_nickname' LIMIT 1
SQL (0.7ms) INSERT INTO "customers" ("name", "surname", "nickname", "email", "encrypted_password", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING "id" [["name", "Kellen"], ["surname", "Okuneva"], ["nickname", "taken_nickname"], ["email", "marielle_robel@howe.co.uk"], ["encrypted_password", "$2a$04$hep6hAUJuFHmgC7CJRHUs.p6uGCKcnNCRoM9Yya22vIWBS1VcFBie"], ["created_at", "2015-11-27 15:45:19.065594"], ["updated_at", "2015-11-27 15:45:19.065594"]]
(0.4ms) RELEASE SAVEPOINT active_record_1
Customer Exists (0.7ms) SELECT 1 AS one FROM "customers" WHERE "customers"."nickname" = 'earlene.rau' LIMIT 1
(0.4ms) ROLLBACK
it is invalidy without password confirmation (FAILED - 2)
(0.2ms) BEGIN
(0.2ms) COMMIT
(0.2ms) BEGIN
(0.3ms) SAVEPOINT active_record_1
Customer Exists (1.2ms) SELECT 1 AS one FROM "customers" WHERE "customers"."nickname" = 'freddie' LIMIT 1
SQL (0.6ms) INSERT INTO "customers" ("name", "surname", "nickname", "email", "encrypted_password", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING "id" [["name", "Camila"], ["surname", "Paucek"], ["nickname", "freddie"], ["email", "taken_mail"], ["encrypted_password", "$2a$04$hoJI92toiYmt/4nBQ8cBGOsXxhb.oPSgPRYz3ZG25IosSnKLkW6nu"], ["created_at", "2015-11-27 15:45:19.093570"], ["updated_at", "2015-11-27 15:45:19.093570"]]
(0.4ms) RELEASE SAVEPOINT active_record_1
(0.3ms) SAVEPOINT active_record_1
Customer Exists (0.7ms) SELECT 1 AS one FROM "customers" WHERE "customers"."nickname" = 'taken_nickname' LIMIT 1
SQL (0.5ms) INSERT INTO "customers" ("name", "surname", "nickname", "email", "encrypted_password", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING "id" [["name", "Johan"], ["surname", "Trantow"], ["nickname", "taken_nickname"], ["email", "betsy.kub@grahamaufderhar.co.uk"], ["encrypted_password", "$2a$04$5MD74LOZqAuYflxbzeTAU.ZzY9trd26IBK4Ah.PLoTeelOqSGAAKy"], ["created_at", "2015-11-27 15:45:19.108486"], ["updated_at", "2015-11-27 15:45:19.108486"]]
(0.3ms) RELEASE SAVEPOINT active_record_1
Customer Exists (0.6ms) SELECT 1 AS one FROM "customers" WHERE "customers"."nickname" = 'sunny.kovacek' LIMIT 1
(0.3ms) ROLLBACK
it is invalidy if password and password_confirmation do not match (FAILED - 3)
(0.4ms) BEGIN
(0.4ms) COMMIT
(0.4ms) BEGIN
(0.3ms) SAVEPOINT active_record_1
Customer Exists (0.6ms) SELECT 1 AS one FROM "customers" WHERE "customers"."nickname" = 'alexandrine' LIMIT 1
SQL (0.5ms) INSERT INTO "customers" ("name", "surname", "nickname", "email", "encrypted_password", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING "id" [["name", "Elna"], ["surname", "Lowe"], ["nickname", "alexandrine"], ["email", "taken_mail"], ["encrypted_password", "$2a$04$vdlgun.EqNJ1J9V7BO5ra.xCU1KZxCq4rjOTurlFiiwafBMe0WysS"], ["created_at", "2015-11-27 15:45:19.137614"], ["updated_at", "2015-11-27 15:45:19.137614"]]
(0.3ms) RELEASE SAVEPOINT active_record_1
(0.3ms) SAVEPOINT active_record_1
Customer Exists (0.9ms) SELECT 1 AS one FROM "customers" WHERE "customers"."nickname" = 'taken_nickname' LIMIT 1
SQL (0.6ms) INSERT INTO "customers" ("name", "surname", "nickname", "email", "encrypted_password", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING "id" [["name", "Julius"], ["surname", "Ruecker"], ["nickname", "taken_nickname"], ["email", "lizzie@steuberwest.us"], ["encrypted_password", "$2a$04$TtVbUrUKkjRAkB6nGWGZAeMIhDPTccHHc1RHF2F4IcDy88gU2kpyS"], ["created_at", "2015-11-27 15:45:19.178343"], ["updated_at", "2015-11-27 15:45:19.178343"]]
(0.3ms) RELEASE SAVEPOINT active_record_1
Customer Exists (0.8ms) SELECT 1 AS one FROM "customers" WHERE "customers"."nickname" = 'eldred.green' LIMIT 1
(0.4ms) ROLLBACK
it is invalidy with an already used email (FAILED - 4)
(0.3ms) BEGIN
(0.2ms) COMMIT
(0.2ms) BEGIN
(0.3ms) SAVEPOINT active_record_1
Customer Exists (0.8ms) SELECT 1 AS one FROM "customers" WHERE "customers"."nickname" = 'ron' LIMIT 1
SQL (0.7ms) INSERT INTO "customers" ("name", "surname", "nickname", "email", "encrypted_password", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING "id" [["name", "Tito"], ["surname", "Barton"], ["nickname", "ron"], ["email", "taken_mail"], ["encrypted_password", "$2a$04$.Fy4UJqU7yPMqpv.vg7VM.KjZJSJ1ECsok8S8FGUw.19tVvT0PuQq"], ["created_at", "2015-11-27 15:45:19.207736"], ["updated_at", "2015-11-27 15:45:19.207736"]]
(0.3ms) RELEASE SAVEPOINT active_record_1
(0.3ms) SAVEPOINT active_record_1
Customer Exists (0.9ms) SELECT 1 AS one FROM "customers" WHERE "customers"."nickname" = 'taken_nickname' LIMIT 1
SQL (0.8ms) INSERT INTO "customers" ("name", "surname", "nickname", "email", "encrypted_password", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING "id" [["name", "Jacinthe"], ["surname", "Thompson"], ["nickname", "taken_nickname"], ["email", "malika_bradtke@price.us"], ["encrypted_password", "$2a$04$hx2GUv5eqTHKJTXuL6OZTOqB1cP5/6gER05SKXCLosoONtehnLm1i"], ["created_at", "2015-11-27 15:45:19.223800"], ["updated_at", "2015-11-27 15:45:19.223800"]]
(0.4ms) RELEASE SAVEPOINT active_record_1
Customer Exists (0.7ms) SELECT 1 AS one FROM "customers" WHERE "customers"."nickname" = 'taken_nickname' LIMIT 1
(0.3ms) ROLLBACK
it is invalidy with an already used nickname
Failures:
1) Customer it is invalidy without email
Failure/Error: expect(FactoryGirl.build(:customer, email: nil)).to_not be_valid
expected #<Customer id: nil, email: nil, encrypted_password: "$2a$04$QccYinmB2OkI09/BkPBKY.Jp7zuygysDBc3PFfqQSqi...", name: "Camren", surname: "Ortiz", nickname: "laurine_boyer", reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: nil, sign_in_count: 0, current_sign_in_at: nil, last_sign_in_at: nil, current_sign_in_ip: nil, last_sign_in_ip: nil, created_at: nil, updated_at: nil, menu_visualization_type: true> not to be valid
# ./spec/models/customer_spec.rb:23:in `block (2 levels) in <top (required)>'
2) Customer it is invalidy without password confirmation
Failure/Error: expect(FactoryGirl.build(:customer, password_confirmation: nil)).to_not be_valid
expected #<Customer id: nil, email: "josiane.abbott@orn.info", encrypted_password: "$2a$04$K7aJMMwMsGNawdX4rMIg0uZkD0IiuNEN3Y6BlnMeUtr...", name: "Emmett", surname: "Crona", nickname: "earlene.rau", reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: nil, sign_in_count: 0, current_sign_in_at: nil, last_sign_in_at: nil, current_sign_in_ip: nil, last_sign_in_ip: nil, created_at: nil, updated_at: nil, menu_visualization_type: true> not to be valid
# ./spec/models/customer_spec.rb:26:in `block (2 levels) in <top (required)>'
3) Customer it is invalidy if password and password_confirmation do not match
Failure/Error: expect(FactoryGirl.build(:customer, password_confirmation: 'antonioross')).to_not be_valid
expected #<Customer id: nil, email: "roselyn@streich.us", encrypted_password: "$2a$04$N0Pdll6HtrBf1bbv7K4g/eCUDeBD41SgtLMC8QSp9Ve...", name: "Stephania", surname: "O'Reilly", nickname: "sunny.kovacek", reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: nil, sign_in_count: 0, current_sign_in_at: nil, last_sign_in_at: nil, current_sign_in_ip: nil, last_sign_in_ip: nil, created_at: nil, updated_at: nil, menu_visualization_type: true> not to be valid
# ./spec/models/customer_spec.rb:29:in `block (2 levels) in <top (required)>'
4) Customer it is invalidy with an already used email
Failure/Error: expect(FactoryGirl.build(:customer, email: 'taken_mail')).to_not be_valid
expected #<Customer id: nil, email: "taken_mail", encrypted_password: "$2a$04$m2ZUU3zA75.FKel0wyXEEu8gukmM3V/R1ZORwpzweZb...", name: "Adele", surname: "Morar", nickname: "eldred.green", reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: nil, sign_in_count: 0, current_sign_in_at: nil, last_sign_in_at: nil, current_sign_in_ip: nil, last_sign_in_ip: nil, created_at: nil, updated_at: nil, menu_visualization_type: true> not to be valid
# ./spec/models/customer_spec.rb:33:in `block (2 levels) in <top (required)>'
Finished in 2.6 seconds (files took 5.69 seconds to load)
8 examples, 4 failures
Failed examples:
rspec ./spec/models/customer_spec.rb:22 # Customer it is invalidy without email
rspec ./spec/models/customer_spec.rb:25 # Customer it is invalidy without password confirmation
rspec ./spec/models/customer_spec.rb:28 # Customer it is invalidy if password and password_confirmation do not match
rspec ./spec/models/customer_spec.rb:32 # Customer it is invalidy with an already used email
我很困惑,一些测试工作有些不
答案 0 :(得分:0)
我同意 bu-oz ,验证Devise管理的属性是多余的,但如果你愿意,你可以这样做:
it 'is invalid without email' do
expect{ User.create!(FactoryGirl.attributes_for(:user, email: nil)) }.to raise_error(ActiveRecord::RecordInvalid)
end
除了异常类之外,您还可以将异常消息传递给raise_error
,以确保记录无效的原因。