Ruby Rails学习中:关于测试的补充,MiniTest报告程序,Guard自动测试
一. 关于测试的补充
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自动测试的更多相关文章
- Ruby Rails学习中:Sass 和 Asset Pipeline,布局中的链接(Rails路由,具名路由),用户注册: 第一步
接上篇: 一.Sass 和 Asset Pipeline Rails 中最有用的功能之一是 Asset Pipeline, 它极大地简化了静态资源文件(CSS.JavaScript 和图像)的生成和管 ...
- Ruby Rails学习中:调试信息和 Rails 的三种环境,Users 资源,调试器,Gravatar 头像和侧边栏
注册 一.调试信息和 Rails 环境 现在咱们要实现的用户资料页面是我们这个应用中第一个真正意义上的动态页面.虽然视图的代码不会动态改变, 不过每个用户资料页面显示的内容却是从数据库中读取的.添加动 ...
- Ruby Rails学习中:添加安全密码
接上篇 一. 添加安全密码 我们已经为 name 和 email 字段添加了验证规则, 现在要加入用户所需的最后一个常规属性: 安全密码.每个用户都要设置一个密码(还要二次确认), 数据库中则存储经过 ...
- Ruby Rails学习中:User 模型,验证用户数据
用户建模 一. User 模型 实现用户注册功能的第一步是,创建一个数据结构,用于存取用户的信息. 在 Rails 中,数据模型的默认数据结构叫模型(model,MVC 中的 M).Rails 为解决 ...
- Ruby Rails学习中:注册表单,注册失败,注册成功
接上篇 一. 注册表单 用户资料页面已经可以访问了, 但内容还不完整.下面我们要为网站创建一个注册表单. 1.使用 form_for 注册页面的核心是一个表单, 用于提交注册相关的信息(名字.电子邮件 ...
- Ruby Rails学习中:Ruby内置的辅助方法,基础内容回顾补充
一. Ruby内置的辅助方法 1.打开文件:app/views/layouts/application.html.erb(演示应用的网站布局) 来咱把注意力放在圈起来的那一行: 这行代码使用 Rail ...
- Ruby Rails学习中:登陆
登陆 一. Sessions 控制器 登录和退出功能由 Sessions 控制器中相应的 REST 动作处理 : 登录表单在 new 动作中处理, 登录的过程是向 create 动作发送 POST 请 ...
- Ruby Rails学习中:有点内容的静态页面
续上篇: 一. 有点内容的静态页面 rails new 命令创建了一个布局文件, 不过现在最好不用.我们重命名这个文件: $ mv app/views/layouts/application.html ...
- Ruby Rails学习中:网站导航,Bootstrap和自定义的CSS,局部视图
添加一些结构 一.网站导航 1.添加一些结构后的网站布局文件 打开文件:app/views/layouts/application.html.erb 简单介绍一下,添加的代码: 我们从上往下看一下这段 ...
随机推荐
- Ajax传递复杂对象报415
特别提示:本人博客部分有参考网络其他博客,但均是本人亲手编写过并验证通过.如发现博客有错误,请及时提出以免误导其他人,谢谢!欢迎转载,但记得标明文章出处:http://www.cnblogs.com/ ...
- redis事务机制和分布式锁
Redis事务机制 严格意义来讲,Redis的事务和我们理解的传统数据库(如mysql)的事务是不一样的:Redis的事务实质上是命令的集合,在一个事务中要么所有命令都被执行,要么所有事物都不执行. ...
- DisplayUtils
import android.app.Activity; import android.content.Context; import android.graphics.Rect; import an ...
- GBK格式字符串右补空格
public class Test2 { public static void main(String[] s) throws IOException { List<User> l ...
- Oracle动态执行脚本创建序号
-----------------------动态创建序列的脚本--------------------------- declare type num_list ) ); -- 老师表.学生表 xz ...
- Kafka管理与监控——调优
1.JVM参数配置优化 如果使用的CMS GC算法,建议JVM Heap不要太大,在4GB以内就可以.JVM太大,导致Major GC或者Full GC产生的“stop the world”时间过长, ...
- Fidessa
Fidessa这样为券商提供交易系统和与交易所连接的公司被称作Independent Software Vendor, 同类的还有FIS(前SunGuard), Bloomberg(AIM), Tho ...
- Es性能优化
1. Es中10亿级别的数据量,如何提高查询效率 (1) 性能优化关键:file system cache a. 不要期待随手挑一个参数,就可以万能的应对所有性能慢的场景 b. es依赖于底层的fil ...
- Linux下Apache虚拟主机配置
Linux下Apache虚拟主机的三种配置.这样可以实现一台主机架构多个独立域名网站.其中基于域名的最为常见.性价比也最高.下面PHP程序员雷雪松详细的讲解下Linux下Apache虚拟主机配置的具体 ...
- windows端口映射
1. 查看netsh interface portproxy show all 2. 添加端口映射转发netsh interface portproxy add v4tov4 listenaddres ...