我在这张幻灯片上阅读了有关 RSpec 最佳实践 ( http://blog.bandzarewicz.com/slides/krug-the-perfect-rspec/#19 ) 和许多其他地方的内容,最好的做法是只对一个期望有一个期望“它” 。例如 :
describe UsersController, '#create' do
# setup spec...
it 'creates a new user' do
should assign_to(:user).with(user)
should set_the_flash
should respond_with(:redirect)
should redirect_to(admin_user_path(user))
end
end
对比
describe UsersController, '#create' do
# setup spec...
it { should assign_to(:user).with(user) }
it { should set_the_flash }
it { should respond_with(:redirect) }
it { should redirect_to(admin_user_path(user)) }
end
为什么最好的做法是对一个“它”只有一个期望?