BDD on Rails - 社区更支持 Shoulda 还是 RSpec?
对于一个新的应用程序,我想开始涉足 BDD,并且我正在尝试在使用 RSpec 还是 Thoughtbot 的 Shoulda 之间做出决定。我喜欢 Shoulda 使用的宏,事实上它似乎并没有重新发明 Ruby/Rails 进行测试的方式,而只是提供了一个附加组件。另一方面,这些宏似乎有点太多“魔力”,而不是明确说明您正在测试的内容(但是我从涉猎中知道,在上面写一打“没有 xxx 应该无效”两行代码很烦人)一个模型)。老实说,我发现为模型编写规范/测试非常简单,几乎无聊,但我发现为控制器编写规范/测试非常困难,因为我永远不确定我应该测试什么或如何编写它。
我对模拟和存根的主题持怀疑态度,因为我认为它们给了你错误的假设(因为你可以告诉它认为它拥有你需要的任何数据或假装方法 X 被调用)并且我知道 RSpec 使重两者的使用。我喜欢 RSPec 生成的文档,但我正在创建一个用于销售的应用程序,而不是提供给客户,因此漂亮的文档没有那么有用。我喜欢 Cucumber,但它似乎有点矫枉过正(是的,我知道它可以与 Shoulda 一起使用)。
此时Rails 社区是支持RSpec 还是应该支持?
For a new application I want to start dabbling in BDD and I'm trying to decide between using RSpec or Thoughtbot's Shoulda. I like the macros that Shoulda uses, and the fact that it doesn't seem to reinvent the way Ruby/Rails does testing, but simply provides an add-on. On the other hand, the macros seem like a bit too much "magic" instead of being explicit about what you're testing (however I know from dabbling that it's annoying to write a dozen "should be invalid without xxx" two-liners on a model). To be honest I find writing specifications/tests for models to be trivially and almost boringly easy, but I find writing them for controllers to be insanely difficult because I'm never sure exactly what I should be testing or how to write it.
I'm iffy on the subject of mocking and stubbing since I think they give you false assumptions (since you can just tell it to think it has whatever data you need or to pretend that Method X was called) and I know that RSpec makes heavy use of both of them. I like the documentation that RSPec produces but I'm creating an application for sale, not to give to a client so the pretty documentation isn't that useful. I like Cucumber but it seems like overkill (and yes I know it can be used with Shoulda).
At this point is the Rails community in favor of RSpec or Shoulda?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
关于模拟和存根(以及假货、双打等) - 当您在单元级别进行测试时,可以使用 TDD 或者事后,整个点是告诉它认为它拥有您需要的数据,使用存根。然后您为真实对象编写一个测试,以确保它确实生成该数据。目的是检查被测类的内部行为,而不是其上游连接是否行为正常。这是在单元级别 - 您将在集成或功能/故事/验收测试(或您喜欢的任何更高级别测试名称)中测试端到端行为。
在我看来,模拟对象更多地涉及下游 - 您想要检查被测试的类是否已进行适当的调用 - 您不关心实际发生的任何事情,只是使用正确的参数调用了正确的方法。模拟确实非常适合这一点。 Rspec 有自己的模拟框架,但是 Mocha 和 FlexMock 也被广泛使用。
顺便说一句,这里有很多关于命名法的讨论/解释/辩论/激烈争论。 Martin Fowler(他比大多数人更有资格谈论这个主题)写了一篇开创性的博客文章 澄清一下,我认为这是有道理的。这是另一篇文章,其中有一些示例。
Regarding mocks and stubs (and fakes, doubles and whatnot) - when you're testing at the unit level, either with TDD or after the fact, the whole point is telling it to think it has the data you need, using a Stub. And you write a test for the real object to ensure that it actually produces that data. The intent is to check the internal behaviour of the class under test, not that its upstream connections are behaving properly. That's at the unit level - you will test the end-to-end behaviour in your integration or feature/story/acceptance tests (or whatever flavour of higher-level test name you prefer).
A mock object is, to my mind, more about the downstream - you want to check that the class under test has made the appropriate call - you're not concerned that anything actually happens, just that the right method was called with the right arguments. Mocks are really good for that. Rspec has its own mocking framework, but Mocha and FlexMock are also widely used.
There's been a lot of discussion/explanation/debate/flame-warring about nomenclature here, BTW. Martin Fowler (who is better-qualified than most to pronounce on the subject) wrote a seminal blog post to clarify it and I think it makes sense. Here's another article, with a few examples.
Rails 社区支持 RSpec 和 Shoulda。这取决于开发商。
如果您更喜欢 Shoulda,请使用它。
如果您更喜欢 RSpec,请使用它;)
它们都是具有相似目标的不同库。这并不意味着每个开发人员都必须支持或反对它。这仅意味着您可以使用其中任何一个。
您可以根据自己的喜好(以及与您合作的其他开发人员)做出选择。
The rails community is in favor of both RSpec and Shoulda. It depends of the developer.
If you prefer Shoulda, use it.
If you prefer RSpec, use it ;)
They're both different library with a similar goal. It doesn't mean every developer has to be for or against it. It only means that you can use either of them.
It's up to you to make your choice depending of your preferences (and the other developers you're working with).
您可以在 RSpec 中使用 shoulda 宏。这绝对不太常见,但却是一个不错的选择: http://robots.thoughtbot .com/post/159805987/speculated-with-shoulda。
但正如 Radar 所说,最终你应该尝试不同的库并做出决定。
You can use shoulda macros in RSpec. It is definitely less common, but a great option: http://robots.thoughtbot.com/post/159805987/speculating-with-shoulda.
But as Radar says, ultimately you should try them different libraries and decide.
我将 Shoulda 匹配器与 RSpec 一起使用。两全其美:RSpec 背后的大社区、快速开发以及 Shoulda 匹配器的大量覆盖。
I use Shoulda matchers with RSpec. Best of both worlds: big community behind RSpec, fast development and lots of coverage with Shoulda matchers.
Shoulda 观察者:758。RSpec
观察者:1279。
最终,由你决定你更喜欢哪一个。
Shoulda watchers: 758.
RSpec watchers: 1279.
Ultimately, it's up to you to decide which one you prefer.
据我所知,既然你提到了 BDD,那么 Cucumber 和 RSpec 之间似乎有更自然的匹配。我最喜欢 shoulda 的一点是它的验证宏。在 RSpec 中,有两个选项可以解决这个问题:
您绝对应该选择您觉得最自然的库(测试的表达方式)。对我来说,使用前面提到的选项,更容易放弃 shoulda 选项(至少就其本身而言),我选择了 rspec 和 cucumber。
As far as i can tell, since you mention BDD, there seems to be a more natural match between cucumber and RSpec. The thing i like most about shoulda are its validation-macro's. There are two options to solve that in RSpec:
You should definitely go with which library feels most natural to you (how tests are expressed). For me, with the previously mentioned options, it was easier to discard the shoulda option (on its own at least), and i went for rspec and cucumber.
我当前的测试工具堆栈是:
My current stack of testing tools is:
世界上许多 Rails 开发人员都使用 RSpec,其中一些使用 Shoulda。 DHH,Rails 的首席开发人员更喜欢 Test::Unit 和 Minitest。 Thoughbot 的 Shoulda 构建于 Test::Unit(当然还有 Minitest)和 RSpec 之上。
最终,作为 Rails 热线 志愿者,您将从 RSpec 中获得更多社区支持,并且有大量额外的宝石专门用于改进 RSpec。话虽如此,Minitest 和 Test::Unite 是 Ruby 的核心。
[意见] 如果软件需要“行为流”,我倾向于使用 RSpec;如果需要纯单元功能,我倾向于使用 Minitest(主要是因为 Minitest 的基准库非常简单)。
A lot of the Rails developers out in the world use RSpec, and some of those use Shoulda. DHH, the lead developer of Rails prefers Test::Unit and Minitest. Thoughbot's Shoulda builds on both Test::Unit (And of course Minitest) and RSpec.
Ultimately, as a Rails Hotline volunteer, you'll get more community support out of RSpec and there are a ton of additional gems out there specifically for improving RSpec. With that said Minitest and Test::Unite are core to Ruby.
[OPINION] I tend to use RSpec if the software requires a "behavior flow" and Minitest if it requires pure unit functionality (mostly because Minitest's benchmark library is really simple).