Skip to content

PENGZhaoqing/CourseSelect

Repository files navigation

CourseSelect Build Status

这个样本系统是基于国科大研究生课程 (高级软件工程) 开发的项目,目的是帮助入门者学习RoR (Ruby on Rails),

适合新学者的入手的第一个项目 (演示Demo戳这里),入门者可以在这个样本系统上增加更多的功能:

  • 处理选课冲突、控制选课人数
  • 统计选课学分,学位课等
  • 增加选课的开放、关闭功能
  • 自定义管理员后台
  • 基于OAuth的授权登陆
  • Excel格式的数据导入
  • 绑定用户邮箱(实现注册激活,忘记密码等)
  • 站内查找检索 (课程按分类查找,过滤等)

目前功能:

  • 多角色登陆(学生,老师,管理员)
  • 学生动态选课,退课
  • 老师动态增加,删除课程
  • 老师对课程下的学生添加、修改成绩
  • 权限控制:老师和学生只能看到自己相关课程信息

如果觉得好,给项目点颗星吧~

截图

说明

目前使用的库和数据库:

使用前需要安装Bundler,Gem,Ruby,Rails等依赖环境。

请根据本地系统下载安装postgresql数据库,并运行psql -h localhost检查安装情况。

安装

在终端(MacOS或Linux)中执行以下代码

$ git clone https://github.com/PENGZhaoqing/CourseSelect
$ cd CourseSelect
$ bundle install
$ rake db:migrate
$ rake db:seed
$ rails s 

在浏览器中输入localhost:3000访问主页

使用

1.学生登陆:

账号:student1@test.com

密码:password

2.老师登陆:

账号:teacher1@test.com

密码:password

3.管理员登陆:

账号:admin@test.com

密码:password

账号中数字都可以替换成2,3...等等

Heroku云部署

项目可直接在Heroku上免费部署

1.fork此项目到自己Github账号下

2.创建Heroku账号以及Heroku app

3.将Heroku app与自己Github下的fork的项目进行连接

4.下载配置Heroku CLI命令行工具

5.运行heroku login在终端登陆,检查与heroku app的远程连接情况git config --list | grep heroku,若未检查到相应的app,请看这里

6.运行部署,详情请戳这里

本地测试

本项目包含了部分的测试(integration/fixture/model test),测试文件位于/test目录下。一键运行所有测试使用rake test

PENG-MacBook-Pro:IMS_sample PENG-mac$ rake test
Run options: --seed 15794

# Running:
.........

Finished in 1.202169s, 7.4865 runs/s, 16.6366 assertions/s.

9 runs, 20 assertions, 0 failures, 0 errors, 0 skips

模型测试

以用户模型为例, 位于test/models/user_test.rb, 首先生成一个@user对象,然后assert用户是否有效,这里的调用valid方法会去检查你的模型中的相关的validates语句是否正确,若@user.valid?为false, 那么此assert会报错,代表"should be valid"这条测试没有通过, 单独运行此测试文件使用rake test test/models/user_test.rb

class UserTest < ActiveSupport::TestCase
  # test "the truth" do
  #   assert true
  # end

  def setup
    @user = User.new(name: "Example User", email: "user@example.com", password: "password", password_confirmation: "password")
  end

  test "should be valid" do
    assert  @user.valid?
  end

  ...

end

视图和控制器测试

以用户登录为例,位于test/integration/user_login_test.rb,首先同样生成一个@user模型,这个@user的用户名和密码可以在test/fixtures/users.yml中指定, 然后我们用get方法到达登录页面(sessions_login_path),然后使用post方法提交这个@user的账号密码来登录,如果登录成功,当前应该会跳转至homes控制器下的index方法进行处理,assert_redirected_to能判断这个跳转过程是否发生,然后调用follow_redirect!来紧跟当前的跳转,用assert_template来判读跳转后的视图文件是否为homes/index, 最后在这个视图文件下做一些测试,比如判断这个视图下连接为root_path的个数等等(根据当前登录的角色不同,当前的页面链接会不同,比如admin用户就会有控制面板的链接rails_admin_path,而普通用户没有,因此可以根据链接的个数来判断当前登录用户的角色)

class UserLoginTest < ActionDispatch::IntegrationTest

  def setup
    @user = users(:peng)
  end

  test "login with valid information" do
    get sessions_login_path
    post sessions_login_path(params: {session: {email: @user.email, password: 'password'}})
    assert_redirected_to controller: :homes, action: :index
    follow_redirect!
    assert_template 'homes/index'
    assert_select "a[href=?]", root_path, count: 2
    assert_select "a[href=?]", rails_admin_path, count: 0
  end
end

测试涵盖率检测

我们可以使用simplecov库来检测我们编写的测试对于我们的项目是否完整,步骤如下:

  1. 在Gemfile文件中导入simplecov库:gem 'simplecov', :require => false, :group => :test,然后bundle install安装
  2. 在test/test_helper.rb的最前面加入simplecov的启动代码(这里默认使用rails自带的test框架,simplecov也支持其他测试框架如rspec,那么启动代码导入的位置请参考simplecov的官方文档)
# 注意这里必须在 require rails/test_help 之前加入,否则不会生效
require 'simplecov'
SimpleCov.start 'rails'

ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'

class ActiveSupport::TestCase
  # Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order.
  fixtures :all

  # Add more helper methods to be used by all tests here...
end
  1. 运行rake test,成功后会根目录的coverage下生成一个index.html文件,用浏览器打开能看到结果如下:

Travis CI 线上自动测试

上述为本地测试,我们可以使用Travis CI来实现自动测试,首先申请一个Travis CI的账号,然后与自己的github连接起来,接着在自己项目根目录中增加一个新的文件.travis.yml如下,这个文件中指定了测试需要的ruby版本,数据库等配置以及一些测试前的脚本操作,当你的github发生更新后,Travis CI会自动触发测试(需要你在Travis CI中自己设置自动/手动触发),然后读取你的.travis.yml文件配置进行测试,其实也就是把本地测试拉到服务器上进行,测试成功后会在你的github项目给一个buliding pass的标签(见CourseSelect题目旁边),代表当前的代码是通过测试的

language: ruby

rvm:
  - 2.2

env:
  - DB=pgsql

services:
  - postgresql

script:
  - RAILS_ENV=test bundle exec rake db:migrate --trace
  - bundle exec rake db:test:prepare
  - bundle exec rake

before_script:
  - cp config/database.yml.travis config/database.yml
  - psql -c 'create database courseselect_test;' -U postgres

How to Contribute

先fork此项目,在分支修改后,pull request到主分支

提问请到issues里创建,欢迎contributor!

About

校园选课系统样本 (a template for course selection system by Ruby on Rails)

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published