一. 关于测试的补充

1.MiniTest报告程序

为了让 Rails 应用的测试适时显示红色和绿色,我建议你在测试辅助文件中加入以下内容:

(1).打开文件:test/test_helper.rb

修改之后,再测试时的样子将会是:

2.使用Guard自动测试

使用 rails test 命令有一点很烦人, 总是要切换到命令行然后手动运行测试。为了避免这种不便, 我们可以使用 Guard 自动运行测试。Guard 会监视文件系统的变动, 假如你修改了 static_pages_controller_test.rb文件, 那么 Guard 只会运行这个文件中的测试。而且, 我们还可以配置 Guard,让它在 home.html.erb 文件被修改后,也自动运行 static_pages_controller_test.rb 文件中的测试。项目文件中的 Gemfile 已经包含了 guard gem,所以我们只需初始化即可:

$ bundle exec guard init

注:然后,编辑生成的 Guardfile 文件,让 Guard 在集成测试和视图发生变化后运行正确的测试,如下代码所示。为了尽量提高灵活性,我建议使用这里的 Guardfile :railstutorial.org/guardfile。注意,默认的 Guardfile 在 Cloud9 IDE 中可能不起作用,请参照前一段给出的网址里的第一行注释。

# A sample Guardfile
# More info at https://github.com/guard/guard#readme ## Uncomment and set this to only include directories you want to watch
# directories %w(app lib config test spec features) \
# .select{|d| Dir.exists?(d) ? d : UI.warning("Directory #{d} does not exist")} ## Note: if you are using the `directories` clause above and you are not
## watching the project directory ('.'), then you will want to move
## the Guardfile to a watched dir and symlink it back, e.g.
#
# $ mkdir config
# $ mv Guardfile config/
# $ ln -s config/Guardfile .
#
# and, you'll have to watch "config/Guardfile" instead of "Guardfile" guard :minitest, spring: "bin/rails test", all_on_start: false do
# with Minitest::Unit
watch(%r{^test/(.*)\/?test_(.*)\.rb$})
watch(%r{^lib/(.*/)?([^/]+)\.rb$}) { |m| "test/#{m[1]}test_#{m[2]}.rb" }
watch(%r{^test/test_helper\.rb$}) { 'test' }
watch("config/routes.rb") { intergration_test }
watch(%r[^app/models/(.*?)\.rb$]) do |matches|
"test/models/#{matches[1]}_test.rb"
end
watch(%r{^app/controllers/(.*?)_controller\.rb$}) do |matches|
resource_tests(matches[1])
end
watch(%r{^app/views/([^/]*?)/.*\.html\.erb$}) do |matches|
["test/controllers/#{matches[1]}_controller_test.rb"] +
integration_tests(matches[1])
end
watch(%r{^app/helpers/(.*?)_helper\.rb$}) do |matches|
integration_tests(matches[1])
end
watch('app/views/layouts/application.html.erb') do
'test/integration/site_layout_test.rb'
end
watch('app/helpers/sessions_helper.rb') do
integration_tests << 'test/helpers/sessions_helper_test.rb'
end
watch('app/controllers/sessions_controller.rb') do
['test/controllers/sessions_controller_test.rb',
'test/integration/users_login_test.rb']
end
watch('app/controllers/account_activations_controller.rb') do
'test/integration/users_signup_test.rb'
end
watch(%r{app/views/users/*}) do
resource_tests('users') +
['test/integration/microposts_interface_test.rb']
end
end # Returns the integration tests corresponding to the given resource.
def integration_tests(resource = :all)
if resource == :all
Dir["test/integration/*"]
else
Dir["test/integration/#{resource}_*.rb"]
end
end # Returns the controller tests corresponding to the given resource.
def controller_test(resource)
"test/controllers/#{resource}_controller_test.rb"
end # Returns all tests for the given resource.
def resource_tests(resource)
integration_tests(resource) << controller_test(resource)
end

修改后的 Guardfile 文件

下面这行代码会让 Guard 使用 Rails 提供的 Spring 服务器,从而减少加载时间,而且启动时不运行整个测试组件。

guard :minitest, spring: "bin/rails test", all_on_start: false do

使用 Guard 时,为了避免 Spring 和 Git 发生冲突,应该把 spring/ 目录添加到 .gitignore 文件中,让 Git 忽略它

配置好 Guard 之后,应该打开一个新终端窗口(与启动 Rails 服务器的做法一样),在其中执行下述命令(若想退出 Guard,按 Ctrl-D 键):

$ bundle exec guard

注:自己试试吧

。。。

测试已经差不多,等会整点别的

Ruby Rails学习中:关于测试的补充,MiniTest报告程序,Guard自动测试的更多相关文章

  1. Ruby Rails学习中:Sass 和 Asset Pipeline,布局中的链接(Rails路由,具名路由),用户注册: 第一步

    接上篇: 一.Sass 和 Asset Pipeline Rails 中最有用的功能之一是 Asset Pipeline, 它极大地简化了静态资源文件(CSS.JavaScript 和图像)的生成和管 ...

  2. Ruby Rails学习中:调试信息和 Rails 的三种环境,Users 资源,调试器,Gravatar 头像和侧边栏

    注册 一.调试信息和 Rails 环境 现在咱们要实现的用户资料页面是我们这个应用中第一个真正意义上的动态页面.虽然视图的代码不会动态改变, 不过每个用户资料页面显示的内容却是从数据库中读取的.添加动 ...

  3. Ruby Rails学习中:添加安全密码

    接上篇 一. 添加安全密码 我们已经为 name 和 email 字段添加了验证规则, 现在要加入用户所需的最后一个常规属性: 安全密码.每个用户都要设置一个密码(还要二次确认), 数据库中则存储经过 ...

  4. Ruby Rails学习中:User 模型,验证用户数据

    用户建模 一. User 模型 实现用户注册功能的第一步是,创建一个数据结构,用于存取用户的信息. 在 Rails 中,数据模型的默认数据结构叫模型(model,MVC 中的 M).Rails 为解决 ...

  5. Ruby Rails学习中:注册表单,注册失败,注册成功

    接上篇 一. 注册表单 用户资料页面已经可以访问了, 但内容还不完整.下面我们要为网站创建一个注册表单. 1.使用 form_for 注册页面的核心是一个表单, 用于提交注册相关的信息(名字.电子邮件 ...

  6. Ruby Rails学习中:Ruby内置的辅助方法,基础内容回顾补充

    一. Ruby内置的辅助方法 1.打开文件:app/views/layouts/application.html.erb(演示应用的网站布局) 来咱把注意力放在圈起来的那一行: 这行代码使用 Rail ...

  7. Ruby Rails学习中:登陆

    登陆 一. Sessions 控制器 登录和退出功能由 Sessions 控制器中相应的 REST 动作处理 : 登录表单在 new 动作中处理, 登录的过程是向 create 动作发送 POST 请 ...

  8. Ruby Rails学习中:有点内容的静态页面

    续上篇: 一. 有点内容的静态页面 rails new 命令创建了一个布局文件, 不过现在最好不用.我们重命名这个文件: $ mv app/views/layouts/application.html ...

  9. Ruby Rails学习中:网站导航,Bootstrap和自定义的CSS,局部视图

    添加一些结构 一.网站导航 1.添加一些结构后的网站布局文件 打开文件:app/views/layouts/application.html.erb 简单介绍一下,添加的代码: 我们从上往下看一下这段 ...

随机推荐

  1. win10系统配置FTP

    FTP是一种远程传输协议,支持这种协议的就是FTP服务器.我们可以在自己的PC机上创建一个.然后通过网页就可以访问FTP服务器下的文件夹. 搭建过程 1.首先需要开启FTP服务.在菜单中打开控制面板. ...

  2. IntelliJ IDEA 2017.3 多模块右边栏 maven projects,maven项目命名问题

    我新建了一个maven web 模块,命名为cloud-access,可是install的时候,名字突然变为cloud-access Maven Webapp了,我就纳闷了,怎么回事.找了很久没发现原 ...

  3. Kafka 实践

    问题描述 配置 Kafka-client 2.x, Spring-Kafka 默认配置 Kafka 三个partition, 使用KafkaListener按group消费. 现象 某天突然发现两个p ...

  4. LeetCode 128. 最长连续序列(Longest Consecutive Sequence)

    题目描述 给定一个未排序的整数数组,找出最长连续序列的长度. 要求算法的时间复杂度为 O(n). 示例: 输入: [100, 4, 200, 1, 3, 2] 输出: 4 解释: 最长连续序列是 [1 ...

  5. 用gcov来检查Qt C++程序的代码覆盖率

      最近才发现MinGW里面包含一个叫做gcov的工具,可以用来检查你的程序运行时调用了哪些代码,同时显示代码行被调用的次数.这个功能在代码的覆盖率和性能调优方便都能用上. 我的运行环境 Window ...

  6. 封装带SSH跳板机的MYSQL

    一.封装带SSH跳板机的MYSQL 二.配置settting import pymysql from sshtunnel import SSHTunnelForwarder class MyDb(ob ...

  7. Python接口测试-模块引用与映射

    PyCharm中发现模块引用老是有各种问题 可以用映射来解决,例如需要调用登录模块里面的东西的时,可以这样处理: 登录模块:1-login.py import this import requests ...

  8. 初步理解js作用域

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  9. centos6里面装zabbix(五)

    今天说使用ICMP ping监控server与agent端的网络状态 今天要使用的是fping,这个软件包需要去官网下载,官网地址:http://www.fping.org/.现在的最新版是4.0 第 ...

  10. backbone之collection

    最近要用到backbone.js,网上也找了些资料,但是就看到一个开头还可以,往下看基本就看不下去了,幸好有这本书[LeanpubRead] Backbone.Marionette.js A Gent ...