关于设置工作 Guard-RSpec 示例项目的问题 [英] Questions about setting up a working Guard-RSpec example project

查看:47
本文介绍了关于设置工作 Guard-RSpec 示例项目的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 guard-rspec 设置一个可运行的 ruby​​ 项目.

I'm trying to set up a working ruby project with guard-rspec.

我已经混合了一个tutsplus 教程 具有一些新语法,如 RSpec 视频另一篇博文.该项目可以在 stack-overflow-question 分支中找到rel="nofollow noreferrer">github.

I've mixed together the general method of a tutsplus tutorial with some new syntax as described in the RSpec videos and another post. The project can be found in the stack-overflow-question branch on github.

我也在尝试按照 Programming Ruby 1.9.所以最终我希望在我的 lib/example/ 目录中有 example.rb.

I'm also trying to practice namespacing as described in Programming Ruby 1.9. So eventually I'd like to have example.rb in my lib/example/ directory.

我有 4 个关于我的项目的问题:

There's 4 questions I have about my project:

  1. 我需要在我的 Guardfile 中放入什么来查看项目中特定目录的更改?我有几种不同的方式可以在我的 Guardfile 中看到,如标记为:
  1. What do I need to put in my Guardfile to watch specific directories in my project for changes? I've several different ways which can be seen in my Guardfile as comments labelled like:

# 观看特定目录尝试 X

  1. 在我的 Guardfile 中取消注释第 8 行和第 9 行有什么区别(下面取消注释并将其设置为仅包含您想要观看的目录)并添加命名组捕获"如 watch(%r{^lib/(?<path>.+)\.rb$}) { |m|"spec/lib/#{m[:path]}_spec.rb" }这里

  1. What is the difference between uncommenting lines 8 and 9 in my Guardfile (bellow Uncomment and set this to only include directories you want to watch) and adding "named group captures" like watch(%r{^lib/(?<path>.+)\.rb$}) { |m| "spec/lib/#{m[:path]}_spec.rb" } as described here

我需要在哪里放置 --clear 选项以使其持久化?我知道我可以像 bin/guard --clear 这样的 binstub 运行它,我已经尝试将它放在我的 Guardfile 中,但是它没有用.

Where do I need to put the --clear option to make it persistent? I know I can run it with my binstub like bin/guard --clear and I've tried putting that in my Guardfile however it didn't work.

您是否在我的项目中看到了任何明显的约定、语法或清晰度错误?我觉得我正在将整个事情拼凑起来,如果能以明智的方式将这件事拼凑在一起,我会很感激的一些指导.

Are there any glaring convention, syntax, or clarity mistakes that you see in my project? I feel like I'm duck taping this whole thing together and I'd appreciate some guidance in piecing this thing together in a sensible way.

推荐答案

我会回答标题中的问题,并在我进行时涵盖子问题".

I'll answer the question in the title and cover the "sub-questions" as I go.

  1. 设置项目的最佳方式(截至目前)是使用 bundle exec guard --init rspec.这应该给出合理的默认值以立即开始工作.它并不完美,但有很多工作计划改进,所以请改为在 GitHub 上提问(更有意义).

  1. The best way to set up a project (as of now) is to use bundle exec guard --init rspec. This should give reasonable defaults to start work immediately. It's not perfect, but there is a lot of work planed to improve things, so do ask questions on GitHub instead (makes more sense).

Guard 必须保持向后兼容性,因此现在有一些非直观"的东西,首先浏览所有文档(和 wiki)是快速了解可用内容和位置的一项很好的投资.在困惑或有疑问时询问也是一个好主意(每个问题一个问题最好).

Guard has to maintain backward compatibility, so there are some "non-intuitive" things for now and skimming through all the docs first (and wiki) is a good investment to quickly know what's available and where. Asking when confused or in doubt is also a good idea (one issue per question is best).

Guard 使用 DSL 来简化侦听器的设置.这有时有点不直观和笨拙,但它确实有助于将操作组织成组,这有助于维护复杂的工作流程.DSL 的命令,例如清除终端自动为:(清除:开启).(这回答了问题 3).

Guard uses a DSL to simplify setting up listeners. This is a bit non-intuitive and clunky at times, but it does help organize actions into groups, which can help maintain complex workflows. The DSL's command for e.g. clearing the terminal automatically is: (clearing: on). (This answers question 3).

有各种各样的用途和场景,所以 Guard 努力让每个人都开心,也在每个平台上.Guard 使用 Listen,它递归地监视目录(主要是由于对 OSX 的限制).这通常不是问题,但对于 OSX 上的大方向,这可能非常非常慢.这就是为什么 Guard 允许您选择要观看的顶级目录(如lib"、app"等).查看整个项目目录非常方便,所以仍然是默认的.有关更多信息,请参见 Listen 项目.因此,默认情况下所有目录(:directories 语句)都是物理监视"(占用操作资源),尽管在 Guard 中您只配置要响应的更改(这就是 watch 语句执行).

There are all kinds of uses and scenarios, so Guard tries hard to make everyone happy and on every platform too. Guard uses Listen, which watches directories recursively (due to restrictions on OSX mostly). This usually isn't a problem, but for huge directions on OSX this can be very, very slow. That's why Guard lets you select which top directories to watch (like 'lib', 'app', etc.). Watching the whole project directory is very convenient, so that is still the default. More info on this is at the Listen project. So by default all directories (:directories statement) are "physically watched" (take up operating resources), though in Guard you only configure which changes you want to respond to (which is what watch statements do).

所选择的词有时有点误导.例如.watch 在 DSL 中的实际意思是:在发生的所有更改中,选择匹配的更改...".传递给 watch 的块被赋予匹配的结果.该块应返回要运行的当前保护插件的文件列表.因此,观察"可能更具误导性而不是帮助.match"可能是一个更好的选择,将来可能会取代watch".

The words chosen are a bit misleading at times. E.g. watch in the DSL actually means: "out of all the changes happening, select changes matching ...". The block passed to watch is given the results of the match. That block should return a list of files for the current guard plugin to run. So "watch" probably is more misleading than helpful. "match" would probably be a better choice and it might replace "watch" in the future.

guard-rspec 项目在更改的文件上运行 RSpec.如果您在调试选项打开的情况下运行警卫,您可以看到确切的 RSpec 命令,例如bundle exec guard -d.为了简化设置,Guard::RSpec 使用一个 DSL,如果您的项目遵循给定的设置,它应该是开箱即用的.例如.dsl.watch_spec_files_for(ruby.lib_files) 已定义 此处:因此,如果您将所有经过测试的源文件放在 lib 中,它几乎已经可以执行您想要的操作.对于其他文件夹,您可以添加自己的文件夹.例如.Rails 项目通常在 app 目录中也有源代码,因此在默认的 Guard::RSpec 模板中有一个声明:dsl.watch_spec_files_for(rails.app_files) 定义了模式如:rails.app_files = %r{^app/(.+)\.rb$} 如果您有一个未涵盖的典型案例,请在该项目中打开一个问题.(涵盖问题 2).

The guard-rspec project runs RSpec on changed files. You can see the exact RSpec commands if you run guard with the debugging option on, e.g. bundle exec guard -d. To simplify setup, Guard::RSpec uses a DSL that should work out-of-the-box if your project follows a given setup. E.g. dsl.watch_spec_files_for(ruby.lib_files) is defined here: so it pretty much already should do what you want if you put all your tested source files in lib. For other folders, you can add your own. E.g. Rails projects typically have sources also in an app directory, so in the default Guard::RSpec template there's an statement: dsl.watch_spec_files_for(rails.app_files) with the pattern defined as: rails.app_files = %r{^app/(.+)\.rb$} If you have a typical case that isn't covered, open an issue in that project. (covers question 2).

示例项目中的一切看起来都很好,建议主要基于品味或偏好.例如

Everything looks fine in the example project and suggestions are mostly based on taste or preference. E.g.

  • 代替 let (:greeter) { Example::RSpecGreeter.new },我会使用 RSpec 的隐式 subject(但有些人可能会争辩说它不太明确且可读性较差)
  • 如果您正在测试 Example::RSpecGreeter,我建议将该类放在一个单独的文件中并改为包含它(require 'example/rspec_greeter')
  • 您可以添加 RubCop/Guard::RuboCop 以检测约定问题(涵盖第 4 个问题)
  • 您可能想查看 Guard 自己的 Guardfile 用于测试本身用于更真实"的设置,没有文档混乱.
  • 为了获得最佳效果,我认为最好复制整个现有项目并重命名要更改的部分.通常需要花费很多时间来微调诸如 Travis 的备用 RSpec 配置、特殊的 gem 发布任务、某些解决方法和便利等.
  • Instead of let (:greeter) { Example::RSpecGreeter.new }, I'd use RSpec's implicit subject (but some may argue that it's less explicit and less readable)
  • if you're testing Example::RSpecGreeter, I'd recommend putting that class in a separate file and including it instead (require 'example/rspec_greeter')
  • you might add RubCop/Guard::RuboCop for detecting convention issues (covers 4th question)
  • you might want to check out Guard's own Guardfile used to test itself for a more "real-life" setup without the documentation clutter.
  • for best results I think it's best to copy a whole existing project and rename the parts you want to change. Usually there are many hours spent still fine-tuning things like alternate RSpec configs for Travis, special gem release tasks, certain workarounds and conveniences, etc.

这篇关于关于设置工作 Guard-RSpec 示例项目的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆