将此RSpec位代码重构为新语法

时间:2014-04-29 15:33:39

标签: ruby-on-rails ruby ruby-on-rails-4 rspec

你会如何使用3.0 RSpec'expect'语法重构这段测试代码?

it "should destroy the micropost" do
  lambda do
    delete :destroy, :id => @micropost
    flash[:success].should =~ /deleted/i
    response.should redirect_to(root_path)
  end.should change(Micropost, :count).by(-1)
end

1 个答案:

答案 0 :(得分:2)

it "destroys the micropost" do
  expect {
    delete :destroy, :id => @micropost
    expect(flash[:success]).to match /deleted/i
    expect(response).to redirect_to(root_path)
  }.to change(Micropost, :count).by(-1)
end

来源:redirect_tomatch and change