From 0a65ac2a358063c61d4b527a83b7c8cff8a852bf Mon Sep 17 00:00:00 2001 From: ccx1024cc <1261138729@qq.com> Date: Sun, 11 Dec 2016 17:40:47 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=AF=B9user=E5=92=8Cproject?= =?UTF-8?q?=E7=9A=84model=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/models/project.rb | 11 +++++++++++ app/models/user.rb | 2 +- spec/factories/factory_helper.rb | 3 +++ spec/factories/projects.rb | 14 ++++++++++++++ spec/factories/users.rb | 4 ++-- spec/models/project_spec.rb | 32 ++++++++++++++++++++++++++++++++ spec/models/user_spec.rb | 4 ++-- spec/rails_helper.rb | 1 + 8 files changed, 66 insertions(+), 5 deletions(-) create mode 100644 spec/factories/factory_helper.rb create mode 100644 spec/factories/projects.rb create mode 100644 spec/models/project_spec.rb diff --git a/app/models/project.rb b/app/models/project.rb index 967bbbc..1635780 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -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 diff --git a/app/models/user.rb b/app/models/user.rb index 751e675..c8cc20e 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -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 } diff --git a/spec/factories/factory_helper.rb b/spec/factories/factory_helper.rb new file mode 100644 index 0000000..2fc678b --- /dev/null +++ b/spec/factories/factory_helper.rb @@ -0,0 +1,3 @@ +def generateRandomString(min_len, max_len) + Faker::Internet.password(min_len, max_len) +end \ No newline at end of file diff --git a/spec/factories/projects.rb b/spec/factories/projects.rb new file mode 100644 index 0000000..c825125 --- /dev/null +++ b/spec/factories/projects.rb @@ -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 diff --git a/spec/factories/users.rb b/spec/factories/users.rb index 19cb43a..82f3993 100644 --- a/spec/factories/users.rb +++ b/spec/factories/users.rb @@ -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 diff --git a/spec/models/project_spec.rb b/spec/models/project_spec.rb new file mode 100644 index 0000000..17d2a16 --- /dev/null +++ b/spec/models/project_spec.rb @@ -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 diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 3ce0d5c..d95e431 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -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 diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb index 6f1ab14..bc7e441 100644 --- a/spec/rails_helper.rb +++ b/spec/rails_helper.rb @@ -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