如何在没有 Shoulda 的情况下在 Rspec 中进行一行测试?
我有一堆非常重复的 rspec 测试,它们都具有相同的格式:
it "inserts the correct ATTRIBUTE_NAME" do
@o.ATTRIBUTE_NAME.should eql(VALUE)
end
如果我可以只进行一行测试,例如:
compare_value(ATTRIBUTE_NAME, VALUE)
但 shoulda 似乎不适合这些类型的测试。还有其他选择吗?
I have a bunch of very repetitive rspec tests that all have the same format:
it "inserts the correct ATTRIBUTE_NAME" do
@o.ATTRIBUTE_NAME.should eql(VALUE)
end
It would be nice if I could just make one line tests like:
compare_value(ATTRIBUTE_NAME, VALUE)
But shoulda doesn't seem to be geared toward these types of tests. Are there other alternatives?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
有时我很遗憾将
subject
暴露为最终用户设备。引入它是为了支持扩展(例如shoulda
匹配器),因此您可以编写如下示例:但是,这样的示例读起来不太好:
如果您要使用
subject 显式,然后在示例中显式引用它,我建议使用
specify
而不是it
:底层语义是相同的,但是这个 ^^ 可以大声朗读。
Sometimes I regret exposing
subject
as an end-user device. It was introduced to support extensions (likeshoulda
matchers), so you can write examples like:Examples like this, however, do not read well:
If you're going to use
subject
explicitly, and then reference it explicitly in the example, I recommend usingspecify
instead ofit
:The underlying semantics are the same, but this ^^ can be read aloud.
如果你希望它读起来更清楚并且只有 1 行,我会编写一个自定义的 RSpec 助手。假设我们要测试以下类:
我们可以编写以下匹配器:
然后要编写测试,我们可以执行以下操作:
如果将所有这些放入文件中,调用 matcher.rb 并运行它,则输出如下:
I would write a custom RSpec helper if you want it to read more clearly and be only 1 line. Suppose we have the following class we want to test:
We could write the following matcher:
Then to write the tests we could do something like:
If you put all of this in a file call matcher.rb and run it the following is output:
我发现这个效果很好:
I found this which works great: