【发布时间】:2014-01-27 13:19:14
【问题描述】:
有没有办法重构它以避免重复:
post :create, user: attributes_for(:user)
鉴于第一个断言需要将其包装在 expect 块中,我看不到将其移动到 before 块的方法。显然,我可以将最后两个断言包装在 context 或 describe 块中,并带有自己的 before 块,但这感觉不对。
context 'with valid attributes' do
it 'should create a new User and save it to the database' do
expect {
post :create, user: attributes_for(:user)
}.to change(User, :count).by(1)
end
it {
post :create, user: attributes_for(:user)
should redirect_to(user_path(assigns[:user]))
}
it {
post :create, user: attributes_for(:user)
should set_the_flash[:notice]
}
end
【问题讨论】:
标签: ruby-on-rails rspec ruby-on-rails-4 refactoring shoulda