私は ActiveAdmin を使用しており、モデルで 2 つのメソッドを呼び出す必要があるコントローラーがあります。これらのモデル メソッドはどちらも失敗時にエラーをスローする可能性があるため、最初のメソッドが成功した場合にのみ 2 番目のメソッドを呼び出すようにコントローラーを設定しました。例えば:
member_action :controller_method, :method => :post do
begin
model.method_1!
rescue => e
flash[:error] = "Method1 failed with error #{e}"
else
begin
model.method_2!
rescue => e
flash[:error] "Method1 was successful but Method2 failed with error #{e}"
else
flash[:success] = "Method1 and Method 2 were successful."
end
end
end
そして、私のテストは次のようになります。
it "flashes success" do
expect { post "method1_and_method2" }.to change { flash[:success] }
end
最初のブロックに flash[:success] メッセージを入れてみましたが、うまくいきました。失敗するのは、2 番目のブロックにネストされている場合のみです。自分でテストしたところ、期待どおりに flash[:success] が表示されています。