model 层测试完成
This commit is contained in:
parent
ff7959fdc9
commit
18ae34e73f
|
@ -4,7 +4,9 @@ class Comment < ActiveRecord:: Base
|
|||
validates :content, presence: true
|
||||
validates :mission_id, presence: true
|
||||
|
||||
validates :user_id, presence: true
|
||||
|
||||
validate :user_id_exist, on: :create
|
||||
|
||||
private
|
||||
|
||||
|
@ -16,6 +18,17 @@ class Comment < ActiveRecord:: Base
|
|||
end
|
||||
end
|
||||
|
||||
|
||||
def user_id_exist
|
||||
|
||||
user = User.find_by(id: user_id)
|
||||
if user == nil
|
||||
errors[:user] = "user is not exist"
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
|
||||
# mission 是否存在的验证将放在controller中进行
|
||||
|
||||
# def mission_id_should_be_exist
|
||||
|
|
|
@ -2,15 +2,21 @@ class Mission < ActiveRecord:: Base
|
|||
|
||||
has_many :comments
|
||||
|
||||
|
||||
validates :name, presence: true
|
||||
validates :deadline, presence: true
|
||||
validates :priority, presence: true
|
||||
validates :status, presence: true
|
||||
validates :content, presence: true
|
||||
|
||||
validates :project_id, presence: true
|
||||
|
||||
|
||||
validate :priority_is_among_valid_values
|
||||
validate :status_is_among_valid_values
|
||||
|
||||
validate :project_exists, on: :create
|
||||
|
||||
private
|
||||
|
||||
def priority_is_among_valid_values
|
||||
|
@ -21,13 +27,21 @@ class Mission < ActiveRecord:: Base
|
|||
end
|
||||
|
||||
def status_is_among_valid_values
|
||||
|
||||
values = ["未开始","进行中","已完成","已测试"]
|
||||
|
||||
if !values.include?(status)
|
||||
if status != 1 and status != 2 and status != 3 and status != 4
|
||||
errors[:status] = "status is not among valid values"
|
||||
end
|
||||
end
|
||||
|
||||
def project_exists
|
||||
#puts project_id
|
||||
project = Project.find_by(id: project_id)
|
||||
#puts project.inspect
|
||||
if project == nil
|
||||
errors[:project_id] = "project_id is not exist"
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
|
||||
end
|
||||
|
|
|
@ -1,8 +1,30 @@
|
|||
class Mission_user <ActiveRecord:: Base
|
||||
class Missions_user <ActiveRecord:: Base
|
||||
|
||||
validates :mission_id, presence: true, uniqueness: true
|
||||
validates :user_id, presence: true
|
||||
|
||||
validate :mission_exist, on: :create
|
||||
validate :user_exist, on: :create
|
||||
|
||||
private
|
||||
def mission_exist
|
||||
|
||||
mission = Mission.find_by(id: mission_id)
|
||||
if mission == nil
|
||||
errors[:mission] = "mission belong to Missions_user is not exist"
|
||||
end
|
||||
|
||||
|
||||
end
|
||||
|
||||
def user_exist
|
||||
|
||||
user = User.find_by(id: user_id)
|
||||
if user == nil
|
||||
errors[:user] = "user belong to Missions_user is not exist"
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
|
||||
end
|
|
@ -4,8 +4,11 @@ class Note < ActiveRecord:: Base
|
|||
|
||||
validates :content, presence: true
|
||||
validates :user_id, presence: true
|
||||
validates :type, presence: true
|
||||
validates :category, presence: true
|
||||
|
||||
validate :note_has_atleast_one_character
|
||||
validate :note_category_should_among_valid_values
|
||||
validate :user_exist, on: :create
|
||||
|
||||
private
|
||||
def note_has_atleast_one_character
|
||||
|
@ -14,6 +17,23 @@ class Note < ActiveRecord:: Base
|
|||
errors[:content] = "note content should has at least one character"
|
||||
end
|
||||
end
|
||||
|
||||
def note_category_should_among_valid_values
|
||||
|
||||
if category != 1 and category != 2 and category !=3
|
||||
errors[:category] = "note category is not among valid values"
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
def user_exist
|
||||
|
||||
user = User.find_by(id: user_id)
|
||||
if user == nil
|
||||
errors[:user] = "user belong to Note is not exist"
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
|
||||
end
|
|
@ -3,5 +3,27 @@ class Projects_user < ActiveRecord:: Base
|
|||
validates :project_id, presence: true
|
||||
validates :user_id, presence: true
|
||||
|
||||
validate :user_exists , on: :create
|
||||
validate :project_exists, on: :create
|
||||
|
||||
private
|
||||
|
||||
def project_exists
|
||||
|
||||
project = Project.find_by(id: project_id)
|
||||
if project == nil
|
||||
errors[:project] = "project belong to projects_user is not exist"
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
def user_exists
|
||||
|
||||
user = User.find_by(id: user_id)
|
||||
if user == nil
|
||||
errors[:user] = "user belong to projects_user is not exist"
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
|
@ -2,9 +2,14 @@ class Share < ActiveRecord::Base
|
|||
|
||||
validates :content, presence: true
|
||||
|
||||
validate :share_has_atleast_one_character
|
||||
validates :project_id, presence: true
|
||||
|
||||
private
|
||||
validates :user_id, presence: true
|
||||
|
||||
validate :share_has_atleast_one_character
|
||||
validate :user_exists,on: :create
|
||||
validate :project_exists,on: :create
|
||||
private
|
||||
def share_has_atleast_one_character
|
||||
tmp = content.strip
|
||||
if tmp.length < 1
|
||||
|
@ -12,5 +17,21 @@ class Share < ActiveRecord::Base
|
|||
end
|
||||
end
|
||||
|
||||
def user_exists
|
||||
|
||||
user = User.find_by(id: user_id)
|
||||
if user == nil
|
||||
errors[:user] = "user belong to Share is not exist"
|
||||
end
|
||||
|
||||
end
|
||||
def project_exists
|
||||
|
||||
project = Project.find_by(id: project_id)
|
||||
if project == nil
|
||||
errors[:project] = "user belong to Share is not exist"
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
|
|
@ -0,0 +1,8 @@
|
|||
class ChangeNoteFieldTypeToCategory < ActiveRecord::Migration
|
||||
def change
|
||||
|
||||
remove_column :notes, :type, :integer
|
||||
add_column :notes, :category, :integer
|
||||
|
||||
end
|
||||
end
|
|
@ -11,7 +11,7 @@
|
|||
#
|
||||
# It's strongly recommended that you check this file into your version control system.
|
||||
|
||||
ActiveRecord::Schema.define(version: 20161228071511) do
|
||||
ActiveRecord::Schema.define(version: 20161229084919) do
|
||||
|
||||
create_table "comments", force: :cascade do |t|
|
||||
t.text "content"
|
||||
|
@ -44,7 +44,7 @@ ActiveRecord::Schema.define(version: 20161228071511) do
|
|||
t.integer "user_id"
|
||||
t.datetime "created_at", null: false
|
||||
t.datetime "updated_at", null: false
|
||||
t.integer "type"
|
||||
t.integer "category"
|
||||
end
|
||||
|
||||
add_index "notes", ["user_id"], name: "index_notes_on_user_id"
|
||||
|
|
|
@ -0,0 +1,20 @@
|
|||
require 'faker'
|
||||
FactoryGirl.define do
|
||||
factory :note do
|
||||
|
||||
|
||||
sequence(:content){ |n| "This is comment content#{n}"}
|
||||
|
||||
|
||||
|
||||
factory :note_content_nil do
|
||||
content nil
|
||||
end
|
||||
|
||||
factory :note_content_allspace do
|
||||
content " "
|
||||
end
|
||||
|
||||
|
||||
end
|
||||
end
|
|
@ -3,17 +3,26 @@ require 'rails_helper'
|
|||
RSpec.describe Comment , type: :model do
|
||||
|
||||
it 'has a valid comment' do
|
||||
params = {content:"good job",mission_id:123}
|
||||
params = {name: "jaxon",email: "370403444@qq.com",password_digest: "123456",phone: "15652336366"}
|
||||
user = User.new(params)
|
||||
user.save
|
||||
params = {content:"good job",mission_id:123,user_id: user.id}
|
||||
expect(Comment.new(params)).to be_valid
|
||||
end
|
||||
|
||||
it "content should not be empty" do
|
||||
params = {content: nil,mission_id: 123}
|
||||
params = {name: "jaxon",email: "370403444@qq.com",password_digest: "123456",phone: "15652336366"}
|
||||
user = User.new(params)
|
||||
user.save
|
||||
params = {content: nil,mission_id: 123,user_id: user.id}
|
||||
expect(Comment.new(params)).to_not be_valid
|
||||
end
|
||||
|
||||
it "mission_id should not be empty" do
|
||||
params = {content:"good job", mission_id: nil}
|
||||
params = {name: "jaxon",email: "370403444@qq.com",password_digest: "123456",phone: "15652336366"}
|
||||
user = User.new(params)
|
||||
user.save
|
||||
params = {content:"good job", mission_id: nil,user_id: user.id}
|
||||
expect(Comment.new(params)).to_not be_valid
|
||||
end
|
||||
|
||||
|
@ -23,6 +32,25 @@ RSpec.describe Comment , type: :model do
|
|||
expect(comment_with_allspace.errors[:content].size).to eq(1)
|
||||
end
|
||||
|
||||
|
||||
it "user should be exist" do
|
||||
params = {name: "jaxon",email: "370403444@qq.com",password_digest: "123456",phone: "15652336366"}
|
||||
user = User.new(params)
|
||||
user.save
|
||||
expect(user).to be_valid
|
||||
params = {name: "project_1",content: "project content"}
|
||||
project = Project.new(params)
|
||||
project.users << user
|
||||
project.save
|
||||
expect(project).to be_valid
|
||||
params = {name: "mission",deadline: "2016-10-17 10:00:00",priority: 1,status: 2,content: "mission content",project_id: project.id}
|
||||
mission = Mission.new(params)
|
||||
mission.save
|
||||
expect(mission).to be_valid
|
||||
expect(mission.errors[:project_id].size).to eq(0)
|
||||
params = {content: "comment", mission_id: mission.id, user_id: user.id}
|
||||
comment = Comment.new(params)
|
||||
expect(comment).to be_valid
|
||||
|
||||
end
|
||||
|
||||
end
|
|
@ -0,0 +1,61 @@
|
|||
require 'rails_helper'
|
||||
|
||||
RSpec.describe Missions_user , type: :model do
|
||||
|
||||
it "mission should exist" do
|
||||
|
||||
params = {name: "jaxon",email: "370403444@qq.com",password_digest: "123456",phone: "15652336366"}
|
||||
user = User.new(params)
|
||||
user.save
|
||||
expect(user).to be_valid
|
||||
|
||||
params = {name: "project_1",content: "project content"}
|
||||
project = Project.new(params)
|
||||
project.users << user
|
||||
project.save
|
||||
expect(project).to be_valid
|
||||
params = {name: "mission",deadline: "2016-10-17 10:00:00",priority: 1,status: 2,content: "mission content",project_id: project.id}
|
||||
mission = Mission.new(params)
|
||||
mission.save
|
||||
expect(mission).to be_valid
|
||||
|
||||
params = {mission_id: 5,user_id: user.id}
|
||||
missions_user = Missions_user.new(params)
|
||||
missions_user.save
|
||||
expect(missions_user).to_not be_valid
|
||||
expect(missions_user.errors[:mission].size).to eq(1)
|
||||
expect(Missions_user.find_by(user_id: user.id)).to eq(nil)
|
||||
|
||||
|
||||
end
|
||||
|
||||
it "user should exist" do
|
||||
|
||||
params = {name: "jaxon",email: "370403444@qq.com",password_digest: "123456",phone: "15652336366"}
|
||||
user = User.new(params)
|
||||
user.save
|
||||
expect(user).to be_valid
|
||||
|
||||
params = {name: "project_1",content: "project content"}
|
||||
project = Project.new(params)
|
||||
project.users << user
|
||||
project.save
|
||||
expect(project).to be_valid
|
||||
params = {name: "mission",deadline: "2016-10-17 10:00:00",priority: 1,status: 2,content: "mission content",project_id: project.id}
|
||||
mission = Mission.new(params)
|
||||
mission.save
|
||||
expect(mission).to be_valid
|
||||
|
||||
params = {mission_id: mission.id,user_id: 2}
|
||||
missions_user = Missions_user.new(params)
|
||||
missions_user.save
|
||||
#puts missions_user.inspect
|
||||
expect(missions_user).to_not be_valid
|
||||
expect(missions_user.errors[:user].size).to eq(1)
|
||||
expect(Missions_user.find_by(mission_id: mission.id)).to eq(nil)
|
||||
|
||||
|
||||
end
|
||||
|
||||
|
||||
end
|
|
@ -0,0 +1,44 @@
|
|||
require 'rails_helper'
|
||||
|
||||
RSpec.describe Note , type: :model do
|
||||
|
||||
it "has a valid note" do
|
||||
|
||||
params = {name: "jaxon",email: "370403444@qq.com",password_digest: "123456",phone: "15652336366"}
|
||||
user = User.new(params)
|
||||
user.save
|
||||
|
||||
params = {content: "note content",user_id: user.id,category: 1}
|
||||
note = Note.new(params)
|
||||
note.save
|
||||
expect(note).to be_valid
|
||||
|
||||
end
|
||||
|
||||
it "notes content should at least has one character" do
|
||||
|
||||
params = {name: "jaxon",email: "370403444@qq.com",password_digest: "123456",phone: "15652336366"}
|
||||
user = User.new(params)
|
||||
user.save
|
||||
|
||||
params = {content: " ",user_id: user.id,category: 1}
|
||||
note = Note.new(params)
|
||||
note.save
|
||||
expect(note.errors[:content].size).to eq(2)
|
||||
|
||||
end
|
||||
|
||||
it "category should be among valid values" do
|
||||
|
||||
params = {name: "jaxon",email: "370403444@qq.com",password_digest: "123456",phone: "15652336366"}
|
||||
user = User.new(params)
|
||||
user.save
|
||||
params = {content: " ",user_id: user.id,category: 4}
|
||||
note = Note.new(params)
|
||||
note.save
|
||||
expect(note.errors[:category].size).to eq(1)
|
||||
expect(note).to_not be_valid
|
||||
|
||||
end
|
||||
|
||||
end
|
|
@ -0,0 +1,51 @@
|
|||
require 'rails_helper'
|
||||
|
||||
RSpec.describe Projects_user , type: :model do
|
||||
|
||||
it "project should be exist" do
|
||||
|
||||
params = {name: "jaxon",email: "370403444@qq.com",password_digest: "123456",phone: "15652336366"}
|
||||
user = User.new(params)
|
||||
user.save
|
||||
expect(user).to be_valid
|
||||
|
||||
params = {name: "project_1",content: "project content"}
|
||||
project = Project.new(params)
|
||||
project.users << user
|
||||
project.save
|
||||
expect(project).to be_valid
|
||||
|
||||
params = {project_id: 3,user_id: user.id}
|
||||
projects_user = Projects_user.new(params)
|
||||
projects_user.save
|
||||
expect(projects_user).to_not be_valid
|
||||
expect(projects_user.errors[:project].size).to eq(1)
|
||||
expect(Projects_user.find_by(project_id: 3)).to eq(nil)
|
||||
|
||||
|
||||
end
|
||||
|
||||
it "user should be exist" do
|
||||
|
||||
params = {name: "jaxon",email: "370403444@qq.com",password_digest: "123456",phone: "15652336366"}
|
||||
user = User.new(params)
|
||||
user.save
|
||||
expect(user).to be_valid
|
||||
|
||||
params = {name: "project_1",content: "project content"}
|
||||
project = Project.new(params)
|
||||
project.users << user
|
||||
project.save
|
||||
expect(project).to be_valid
|
||||
|
||||
params = {project_id: project.id,user_id: 5}
|
||||
projects_user = Projects_user.new(params)
|
||||
projects_user.save
|
||||
expect(projects_user).to_not be_valid
|
||||
expect(projects_user.errors[:user].size).to eq(1)
|
||||
expect(Projects_user.find_by(user_id: 5)).to eq(nil)
|
||||
|
||||
end
|
||||
|
||||
|
||||
end
|
|
@ -0,0 +1,90 @@
|
|||
require 'rails_helper'
|
||||
|
||||
RSpec.describe Share, type: :model do
|
||||
|
||||
it "its a valid share" do
|
||||
|
||||
params = {name: "jaxon",email: "370403488@qq.com",password_digest: "123456",phone: "15652336366"}
|
||||
user = User.new(params)
|
||||
user.save
|
||||
expect(user).to be_valid
|
||||
|
||||
params = {name: "project_1",content: "project content"}
|
||||
project = Project.new(params)
|
||||
project.users << user
|
||||
project.save
|
||||
expect(project).to be_valid
|
||||
|
||||
params = {content: "share content",project_id: project.id,user_id: user.id}
|
||||
share = Share.new(params)
|
||||
share.save
|
||||
expect(share).to be_valid
|
||||
expect(Share.find_by(id: share.id)).to_not eq(nil)
|
||||
|
||||
end
|
||||
|
||||
it "share content should have at least one character" do
|
||||
|
||||
params = {name: "jaxon",email: "370403444@qq.com",password_digest: "123456",phone: "15652336366"}
|
||||
user = User.new(params)
|
||||
user.save
|
||||
expect(user).to be_valid
|
||||
|
||||
params = {name: "project_1",content: "project content"}
|
||||
project = Project.new(params)
|
||||
project.users << user
|
||||
project.save
|
||||
expect(project).to be_valid
|
||||
|
||||
params = {content: " ",project_id: project.id,user_id: user.id}
|
||||
share = Share.new(params)
|
||||
share.save
|
||||
expect(share).to_not be_valid
|
||||
expect(share.errors[:content].size).to eq(2)
|
||||
expect(Share.find_by(id: share.id)).to eq(nil)
|
||||
|
||||
end
|
||||
|
||||
it "share's project should be exist" do
|
||||
params = {name: "jaxon",email: "370403444@qq.com",password_digest: "123456",phone: "15652336366"}
|
||||
user = User.new(params)
|
||||
user.save
|
||||
expect(user).to be_valid
|
||||
|
||||
params = {name: "project_1",content: "project content"}
|
||||
project = Project.new(params)
|
||||
project.users << user
|
||||
project.save
|
||||
expect(project).to be_valid
|
||||
|
||||
params = {content: "share content",project_id: 5,user_id: user.id}
|
||||
share = Share.new(params)
|
||||
share.save
|
||||
expect(share).to_not be_valid
|
||||
expect(share.errors[:project].size).to eq(1)
|
||||
expect(Share.find_by(id: share.id)).to eq(nil)
|
||||
|
||||
end
|
||||
|
||||
it "share's user should be exist" do
|
||||
params = {name: "jaxon",email: "370403444@qq.com",password_digest: "123456",phone: "15652336366"}
|
||||
user = User.new(params)
|
||||
user.save
|
||||
expect(user).to be_valid
|
||||
|
||||
params = {name: "project_1",content: "project content"}
|
||||
project = Project.new(params)
|
||||
project.users << user
|
||||
project.save
|
||||
expect(project).to be_valid
|
||||
|
||||
params = {content: "share content",project_id: project.id,user_id: 10}
|
||||
share = Share.new(params)
|
||||
share.save
|
||||
expect(share).to_not be_valid
|
||||
expect(share.errors[:user].size).to eq(1)
|
||||
expect(Share.find_by(id: share.id)).to eq(nil)
|
||||
|
||||
end
|
||||
|
||||
end
|
Loading…
Reference in New Issue