2014-02-22 09:54:23 +08:00
|
|
|
# encoding: utf-8
|
2014-02-22 09:44:14 +08:00
|
|
|
require File.expand_path('../../test_helper', __FILE__)
|
|
|
|
|
|
|
|
class CourseTest < ActiveSupport::TestCase
|
2014-07-18 11:08:36 +08:00
|
|
|
fixtures :courses, :roles, :member_roles, :members, :boards, :messages, :news
|
2014-02-22 09:44:14 +08:00
|
|
|
def setup
|
2014-07-18 11:08:36 +08:00
|
|
|
end
|
2014-02-22 09:44:14 +08:00
|
|
|
|
2014-07-21 15:50:40 +08:00
|
|
|
# 基本测试,从不完整的空对象,验证属性的合法值
|
|
|
|
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]
|
|
|
|
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
|
|
|
|
|
2014-07-18 11:08:36 +08:00
|
|
|
test 'name valid with CJK coding' do
|
|
|
|
name_quirk = '电路与电子学基础(软件工程专业)'
|
2014-07-18 15:57:18 +08:00
|
|
|
course = Course.find_by_id(74)
|
2014-07-18 11:08:36 +08:00
|
|
|
course.name = "test_course"
|
|
|
|
assert course.valid?
|
|
|
|
course.name = name_quirk
|
2014-07-23 09:31:58 +08:00
|
|
|
assert course.valid?, "Quirk name ===> #{course.name}"
|
2014-02-22 09:44:14 +08:00
|
|
|
end
|
2014-07-18 11:08:36 +08:00
|
|
|
|
2014-07-21 15:50:40 +08:00
|
|
|
test 'description too long must be valid.(4096)' do
|
2014-07-18 11:08:36 +08:00
|
|
|
course = Course.find_by_id(8)
|
2014-07-21 15:50:40 +08:00
|
|
|
course.description = 'a' * 40 # description length validate.
|
2014-07-18 11:08:36 +08:00
|
|
|
course.valid?
|
2014-07-18 15:57:18 +08:00
|
|
|
assert course.errors[:name].empty?
|
2014-07-18 11:08:36 +08:00
|
|
|
assert course.valid?
|
2014-02-22 09:44:14 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def teardown
|
|
|
|
end
|
|
|
|
end
|