根据我对 AAA - 安排行为断言的理解,重要的是要明确什么代码用于安排、执行和断言。
在测试一个复杂的场景时,我习惯于在每次测试中多次执行相同的操作。排列发生变化,断言也发生变化。但是动作还是一样的。
最近我遇到了这段代码:
before do
post 'somewhere'
end
it 'creates something' do
expect 'something'.to be true
end
而且我不确定这是否是一个好习惯。例如,当我们添加上下文时,安排代码处于动作和断言的中间。
before do
post 'somewhere'
end
[ some tests ]
context 'when a more complex scenario applies' do
before do
[more complex arrangements]
end
it 'creates something more complex' do
expect 'something'.to be true
end
end
干这些动作是一个好习惯吗?