从 Rails 应用程序代码运行 rspec
我遇到了需要验证一些正则表达式的情况。
因此,在我的应用程序运行期间,我可能想测试特定的正则表达式:
- 不包含空格
- 仅包含一定数量的捕获组
- 不使用某些字符
- 仅包含一定数量的通配符
rspec 似乎是执行此操作的完美工具。然而,我意识到它通常用于在应用程序运行之前测试应用程序接口、假设和逻辑。但是,自然语法与自动报告输出相结合会很好。
问题: 这是 rspec 的正确使用吗?
如何从正在运行的应用程序中调用描述?
或者,我应该放弃这种方法并简单地在类中编写方法来执行验证吗?
I've got a situation where I need to validate some regular expressions.
So, during my application run, I may want to test that a particular regex:
- Contains no spaces
- Contains only a certain number of capture groups
- Does not use certain characters
- Only contains a certain number of wildcards
rspec seems like the perfect tool for doing this. I realize that it's typically used to test application interfaces, assumptions and logic before an application is run, however. But, the natural syntax combined with the automatic reporting output would be nice to have.
Questions:
Is this an appropriate use of rspec?
How can one call a description from within a running application?
Or, should I abandon this approach and simply write methods within my class to perform the validations?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
以这种方式使用 rspec 是非常不鼓励和不寻常的。您应该将测试代码留在 Gemfile 的 :test 组中,而不是在应用程序中引用它。
相反,使用 Rails 验证您的字段与正则表达式格式匹配,然后在 rspec 中编写测试来验证您的验证。
Using rspec in this way is highly discouraged and unusual. You should leave testing code in a :test group in your Gemfile and not reference it in your app.
Instead, use rails validations that your field matches a regex format, and then write tests in rspec to verify your validations.
这绝对是新事物:在 Rails 内使用 rspec 进行验证。但对于特定问题,人们倾向于建议使用 DSL,因此 rspec 是一种可能非常适合您的工作的 DSL。
所以如果是这样的话:为什么不呢,是的,继续吧。发挥创造力,找到使用现有工具的新方法。
只是一个小警告:从您标记的几个点来看,复杂性似乎并不太大,因此请确保您没有使用火箭筒来杀死苍蝇。 Rspec 是一个庞大且非常强大的工具,在 Rails 进程中绑定 rspec 来运行可能并不完全简单。
This is definitely something new: using rspec inside rails for validation. But for specific problems one tends to propose to use a DSL, and as such rspec is a DSL which might just perfectly suited for your job.
So if that is the case: why not, yes, go ahead. Be creative and find new ways to use the tools you have.
Just a small warning: from the few points you marked, the complexity does not seem to be too big, so make sure you are not using a bazooka to kill a fly. Rspec is a big and very powerful tool, tying in rspec to run during the rails process might not be entirely straightforward.
如果要生成报告,可以使用全局 after(:all) { puts "report gets here" } 或 after(:each)。如果您预计某些数据会破坏您的测试,则可以测试 .should raise_exception。我想您会编写大量异常处理,以将预期的失败排除在输出之外。将结果记录到数据库或文件中也可能很烦人。如果可以的话,描述您正在对数据进行的测试,然后在最后解析 rspec 的输出。
您可以在下面看到我对失败的测试的描述。
如果这是测试文本和正则表达式,我会很容易地报告失败。
失败/错误:失败(“数据有空格”)
等。If you want to generate a report, you could use the global after(:all) { puts "report goes here" } or after(:each). If you expect some of your data to blow up your tests, you can test for .should raise_exception. I imagine you'd be writing lots of exception handling to keep the expected failures out of the output. Logging the results to a database or a file might also be annoying. If you can, describe the test that you are doing on the data and then just parse the output of rspec at the end.
You can see below that I have a description of the test that failed.
I would be easy enough to just make a report of the failures if this was testing text and regex's.
Failure/Error: fail("Data has spaces")
etc.