一. 关于测试的补充

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. Java异常Error和Exception

    简述 程序运行时,发生了不被期望的结果,阻止了程序按照预期正常执行,这就是异常.世界上没有不出错的程序,只有正确处理好意外情况,才能保证程序的可靠性. Java 语言在设计之初就提供了相对完善的异常处 ...

  2. python获取hive表时间格式最大分区

    #获取表的最大分区 import boto3 from datetime import datetime,timedelta def get_max_partition(db_name,table_n ...

  3. 在Linux下使用rm -rf /*后会怎样?

    每个工作过的码农,也许不知道分布式,也许不知道高并发,但想必都知道这句鼎鼎大名的代码.本人对此也是比较好奇的,不妨用虚拟机试试看 首先是普通角色: 普通角色把拥有权限的文件全都删掉了后,其他文件的提示 ...

  4. redis 用scan 代替keys,hgetAll

    转载自:https://blog.csdn.net/w05980598/article/details/80264568 众所周知,当redis中key数量越大,keys 命令执行越慢,而且最重要的会 ...

  5. css3_1

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

  6. HDU3549:Flow Problem(最大流入门EK)

    #include <stdio.h> #include <string.h> #include <stdlib.h> #include <queue> ...

  7. LC 934. Shortest Bridge

    In a given 2D binary array A, there are two islands.  (An island is a 4-directionally connected grou ...

  8. 使用python装饰器计算函数运行时间的实例

    使用python装饰器计算函数运行时间的实例 装饰器在python里面有很重要的作用, 如果能够熟练使用,将会大大的提高工作效率 今天就来见识一下 python 装饰器,到底是怎么工作的. 本文主要是 ...

  9. JAVA 基础编程练习题9 【程序 9 求完数】

    9 [程序 9 求完数] 题目:一个数如果恰好等于它的因子之和,这个数就称为"完数".例如 6=1+2+3.编程找出 1000 以内的 所有完数. package cskaoyan ...

  10. 数位dp作业

    dp东西实在太多,昨天开了个树形dp入门,还没入呢,今天就要写数位dp,也不知道这种学习状态对不对啊? A - 不要62 题意: 输入n到m内,符合条件的数的个数.条件:不含62和4. 这里直接学习q ...