Benjamin
Benjamin

Reputation: 10393

How to test whether an exception occurs or not in a Controller?

# controller
def index
  p = params[:p]        
  raise ABCException if p
end

# test
test "expect index controller raise ABCException" do
  assert_raise ABCException do
    # how do I write this block?
  end
end

I would like to call the index controller with :p => true to raise an exception. I understand the assert_raise function, but don't know how to call controller as same as an end-user requests.

Upvotes: 0

Views: 37

Answers (1)

moritz
moritz

Reputation: 25757

You can do

assert_raise ABCException do
  get :index, :p => true
end

Upvotes: 2

Related Questions