本文介绍了rspec 和 shoulda - 互补还是替代?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用了 shoulda 一段时间,并且我已经阅读并使用了 rspec.我没有做过深入的比较和对比.但在我看来,两者之间有一些重叠,但它们不是 1-1 替代.

I've used shoulda for a while, and I've read and played with rspec. I have not done an in depth compare and contrast. But it seems to me like there is some overlap between the two, but that they are not 1-1 replacements.

我正在考虑使用 rspec 在我的 rails 系统中编写一些单元测试,而不替换所有使用 shoulda 编写的现有测试.只是为了获得感觉.

I am considering writing some unit tests in my rails system with rspec, without replacing all the existing tests that are written with shoulda. Just as a way to get the feel.

这是个好主意吗?我可以逐渐从一个转移到另一个还是自找麻烦?

Is this a good idea? Can I gradually move from one to the other or am I asking for trouble?

我应该考虑其中一个比另一个明显的优势吗?

Any clear cut advantages of one over the other that I should consider?

谢谢!

推荐答案

我不得不反驳 Chris 的回答,即它们是替代方案.我在 Rails 应用程序中同时使用了 Shoulda 和 Rspec,它们相互补充.

I have to argue against Chris's answer that they are alternatives. I use Shoulda and Rspec together in my Rails application, and they complement each other well.

这个组合让我可以编写简洁的单行单元测试,用于重复发生的事情,比如关联和验证,以及为更复杂的规范提供完整的 rspec 套件.您可以在没有任何冲突的情况下获得两全其美.

This combo allows me to write concise one-line unit tests for recurring things like associations and validations, as well as the having the full rspec suite for more complex specs. You get the best of both worlds without any conflicts.

查看 Shoulda README,其中显示了如何与 Rspec 一起安装.它甚至说它提供了Test::Unit 和 RSpec 兼容的 one-liners,用于测试常见的 Rails 功能.否则这些测试会更长、更复杂且容易出错."

Check out the Shoulda README which shows how to install along side Rspec. It even says it provides "Test::Unit- and RSpec-compatible one-liners that test common Rails functionality. These tests would otherwise be much longer, more complex, and error-prone."

编辑(示例):

在规范的顶部,我总是声明简洁易读的类关系和验证测试.

At the top of my specs, I always declare my Class relationship and validation tests which are concise and easy to read.

describe Component do

  context 'relationships' do
    it { should belong_to(:technology)}
    it { should have_many(:system_components) }
    it { should have_and_belong_to_many(:variables) }
    it { should have_many(:images).dependent(:destroy) }
    it { should have_many(:documents).dependent(:destroy) }
  end

  context 'validations' do
    it { should validate_presence_of(:make) }
    it { should validate_presence_of(:model) }
    it { should ensure_length_of(:name).is_at_most(100) }
    it { should validate_presence_of(:technology_id) }
  end
end

然后我的规范的其余部分将有更复杂的测试,我将使用来自 Rspec 的模拟和存根.

Then the rest of my spec will have more complex tests where I am using mocks and stubs which come from Rspec.

这篇关于rspec 和 shoulda - 互补还是替代?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 23:48