Wyler-Yue / BaoYanTong

国科大高级软件工程项目——保研通

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

<<<<<<< master

BaoYanTong Build Status

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

我们实现了以下的功能:

功能:

  • 进入页面展示
  • 新用户注册账号
  • 多角色登陆(会员,培训师)
  • 会员动态增加新的项目
  • 培训师动态增加,删除项目
  • 培训师的信息修改(如联系方式)

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

截图

imagine

imagine

imagine imagine

说明

目前使用的库和数据库:

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

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

安装

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

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

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

使用

1.会员登陆:

账号:menber1@test.com

密码:password

2.培训师登陆:

账号:trainer1@test.com

密码:password

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!

=======

master

About

国科大高级软件工程项目——保研通

License:MIT License


Languages

Language:Ruby 51.1%Language:HTML 48.0%Language:CSS 0.7%Language:JavaScript 0.3%