我有一些与ActiveRecord的valid?
,invalid?
和errors
相同的“相关”方法;以及我想要练习/展示它们的几个context
。
很明显,有context
vs 方法返回的矩阵。
一种常见的模式/做法是将context
嵌套在describe '#<method>'
个区块内,而不是相反的方向,对吧?我没有在每个方法的context
内复制describe
,而是为'set'尝试了一个块。
但是...... #errors
比其他人更复杂,值得更多关注 - 因此它看起来确实值得describe '#errors'
阻止
describe MyClass do
...
describe '#valid? / #invalid? / #errors' do
context 'all good' do
...
it { should be_valid }
it { should_not be_invalid }
describe '#errors' do
it { should be_an_instance_of( Hash )}
it { should be_empty }
it ...
...
end
end
context 'some bad' do
...
it { should_not be_valid }
it { should be_invalid }
describe '#errors' do
it { should be_an_instance_of( Hash )}
it ...
its ...
...
end
end
end
...
end
context
越多,describe '#errors'
和it { should be_an_instance_of( Hash )}
开始感到重复。
有没有DRYer的方法来做到这一点? 这是“可接受的”非干吗?
TIA
答案 0 :(得分:1)
惯例是describe
用于“事物”而context
用于“状态”所以是的,在我看来,你已经适当地嵌套和使用它们。至于你的其他问题,我不知道干嘛做你做过的事情的方式,是的,我认为你所表现出的是可接受的干嘛。如果您的#errors
个案例之间存在更多共性,则可以引入“共享示例组”,但在这种情况下似乎不需要这样做。
所有这一切,我不认为自己是这个主题的专家,并期待更多的知情意见。