我知道这与实际代码本身并没有真正的关系,但我刚刚开始使用 Rspec,并且在使测试听起来像英语时遇到了麻烦。我认为如果我知道在传递给它的字符串中放入什么并描述块,它将大大提高我的测试质量。前任:
Category
validations
is valid with a title and description
is invalid without a title
is invalid without a description
when it has subcategories
returns the right children subcategories
should be accessible by its subcategories
when it has no subcategories
returns an empty set
这就是我的类别规范中的内容。是否有某种方式/模式我必须编写传递给it
and的字符串describe
?对于您的 Rspec 专家,您在编写字符串时通常会想到什么describe
,这与您编写it
字符串或context
字符串时有何不同?以下是我的规格示例,以防您需要一些实际代码来使用:
describe 'validations' do
let(:category) { Category.new }
it 'is valid with a title and description' do
category.title = 'Category'
category.description = 'Lorem Ipsum'
category.should be_valid
end
it 'is invalid without a title' do
category.description = 'Lorem Ipsum'
category.should_not be_valid
end
it 'is invalid without a description' do
category.title = 'Category'
category.should_not be_valid
end
end
context 'when it has subcategories' do
let(:category) { FactoryGirl.create(:category) }
it 'returns the right children subcategories' do
child = category.subcategories.build(title: 'Child Category', description: 'Lorem Ipsum')
category.subcategories.should include(child)
end
it 'should be accessible by its subcategories' do
child = category.subcategories.build(title: 'Child Category', description: 'Lorem Ipsum')
child.parent_category.should_not be_nil
end
end
context 'when it has no subcategories' do
let(:category) { FactoryGirl.create(:category) }
it 'returns an empty set' do
category.subcategories.should be_empty
end
end