添加对user和project的model测试

This commit is contained in:
ccx1024cc 2016-12-11 17:40:47 +08:00
parent dbeccff926
commit 0a65ac2a35
8 changed files with 66 additions and 5 deletions

View File

@ -1,3 +1,14 @@
class Project < ActiveRecord::Base
has_and_belongs_to_many :users
validate :require_at_least_on_user # 项目中最少要有一个用户
validates :name, presence: true, length: { minimum: 1, maximum: 50 }, uniqueness: true
private
def require_at_least_on_user
if !users || users.size == 0 # 这里使用size可以智能选择需不需要查询数据库
errors[:users] = 'at least one user in the project'
end
end
end

View File

@ -6,7 +6,7 @@ class User < ActiveRecord::Base
has_secure_password # 等价于验证password_confirm和password是否相等并且验证password是否存在
validates :name, presence: true, uniqueness: true, length: { maximum: 20 }
validates :name, presence: true, uniqueness: true, length: { maximum: 30 }
validates :email, presence: true, uniqueness: true, length: { maximum: 50},
format: { with: /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i }
validates :password, length: { minimum: 6 }

View File

@ -0,0 +1,3 @@
def generateRandomString(min_len, max_len)
Faker::Internet.password(min_len, max_len)
end

View File

@ -0,0 +1,14 @@
FactoryGirl.define do
factory :project do
name { Faker::Name.title }
sequence(:content){ |n| "This is project content#{n}"}
after(:build) do |project|
3.times { project.users << FactoryGirl.create(:user_with_sequence_number) }
end
factory :project_name_length_gt_20 do
name { generateRandomString(51, 51)}
end
end
end

View File

@ -10,8 +10,8 @@ FactoryGirl.define do
sequence(:name){ |n| "username#{n}"}
end
factory :user_name_length_gt_20 do
name '12345678901234567890xxxx'
factory :user_name_length_gt_30 do
name generateRandomString(31, 31)
end
factory :user_email_length_gt_50 do

View File

@ -0,0 +1,32 @@
require 'rails_helper'
RSpec.describe Project, type: :model do
it 'has a valid factory' do
expect(build(:project)).to be_valid
end
describe 'users test' do
it 'has at least on user' do
project_with_no_user = build(:project)
project_with_no_user.users = []
project_with_no_user.valid?
expect(project_with_no_user.errors[:users].size).to eq(1)
end
end
describe 'name test' do
it 'does not allow absence of name' do
expect(build(:project, name: nil)).to_not be_valid
end
it 'length of name in [1,20]' do
expect(build(:project_name_length_gt_20)).to_not be_valid
end
it 'does not allow duplicate name' do
create(:project, name: 'project1')
expect(build(:project, name: 'project1')).to_not be_valid
end
end
end

View File

@ -18,8 +18,8 @@ RSpec.describe User, type: :model do
expect(user_duplicated_name.errors[:name].size).to eq(1)
end
it 'does not allow name length > 20' do
expect(build(:user_name_length_gt_20)).not_to be_valid
it 'does not allow name length > 30' do
expect(build(:user_name_length_gt_30)).not_to be_valid
end
end

View File

@ -5,6 +5,7 @@ require File.expand_path('../../config/environment', __FILE__)
abort("The Rails environment is running in production mode!") if Rails.env.production?
require 'spec_helper'
require 'rspec/rails'
require 'factories/factory_helper' # 添加预固件辅助器
# Add additional requires below this line. Rails is not loaded until this point!
# Requires supporting ruby files with custom matchers and macros, etc, in