How to test a failing transaction


#1

Hi,

I was wondering if somebody knows how I can make a transaction fail, so
I can test my rescue code. So a very basic transaction:

  begin
    Listing.transaction(@ad) do

      if @ad.save
        flash[:notice] = 'Ad was successfully created.'
        redirect_to :action => 'list'
      else
        render :action => 'new'
      end
    end
  rescue => error
    flash[:warning] = 'An error has occurred'
    redirect_to :action => 'new'
  end

But I want to test that I get redirected to ‘new’ and have a flash
message. All suggestions are welcome.

Kind regards,

Nick


#2

On 1/7/06, Nick S. removed_email_address@domain.invalid wrote:

        redirect_to :action => 'list'

message. All suggestions are welcome.

Kind regards,

Nick

use @ad.save! and pass in some data to make the data invalid in your
test. Be sure to use the uses_transaction method in your unit test
(or turn off transactional fixtures) when testing this. You typically
can’t have nested transactions (depending on DB I believe).

http://weblog.digett.com/2005/07/06/transactional-fixtures-in-ruby-on-rails/


rick
http://techno-weenie.net