From 04ec6ff27aaf7b82bb5a604b4678620fc9f31cba Mon Sep 17 00:00:00 2001 From: yanxd Date: Mon, 21 Jul 2014 15:50:40 +0800 Subject: [PATCH] course model validations testing. --- test/fixtures/courses.yml | 26 ++++++++++++++++++++++++++ test/unit/course_test.rb | 35 +++++++++++++++++++++++++++++++++-- 2 files changed, 59 insertions(+), 2 deletions(-) diff --git a/test/fixtures/courses.yml b/test/fixtures/courses.yml index a6d8cb763..803c14c26 100644 --- a/test/fixtures/courses.yml +++ b/test/fixtures/courses.yml @@ -1,4 +1,30 @@ --- +courses_invalid_001: + id: 1 + tea_id: + name: '' + state: + code: + time: + extra: + created_at: 2013-09-30 15:36:00.000000000 Z + updated_at: 2014-04-19 01:50:41.000000000 Z + location: + term: + string: + password: + setup_time: + endup_time: + class_period: + school_id: + description: + status: 1 + attachmenttype: 2 + lft: + rgt: + is_public: 1 + inherit_members: 1 + courses_008: id: 8 tea_id: 5 diff --git a/test/unit/course_test.rb b/test/unit/course_test.rb index 86963eb4b..a9544fc05 100644 --- a/test/unit/course_test.rb +++ b/test/unit/course_test.rb @@ -6,6 +6,37 @@ class CourseTest < ActiveSupport::TestCase def setup end + # 基本测试,从不完整的空对象,验证属性的合法值 + test 'test courses all validations' do + course = Course.new + assert_not_nil course + # 默认模型不完整 + assert course.invalid? + error_keys = course.errors.keys + + valid_attr = [:password, :term, :name, :class_period] + puts error_keys + valid_attr.each do |attr| + assert(error_keys.include?(attr), "attr unknow: #{attr}") + end + + # 添加密码 + course.password = '123456' + course.term = '春季学期' + course.name = '课程名称测试' + course.class_period = '32' + assert course.valid?, course.errors.full_messages.to_s + end + + test 'class_period format validations' do + course = Course.find_by_id(8) + assert course, "course obj is nil." + course.class_period = '' + assert course.invalid?, "course class_period can not be number. #{course.class_period}" + course.class_period = '32' + assert course.valid?, "course class_period can not be number. #{course.class_period}" + end + test 'name valid with CJK coding' do name_quirk = '电路与电子学基础(软件工程专业)' course = Course.find_by_id(74) @@ -15,9 +46,9 @@ class CourseTest < ActiveSupport::TestCase assert course.valid? end - test 'description too long must be valid.' do + test 'description too long must be valid.(4096)' do course = Course.find_by_id(8) - course.description = 'a' * 2048 # description length validate. + course.description = 'a' * 40 # description length validate. course.valid? assert course.errors[:name].empty? assert course.valid?