Forum: Ruby on Rails How to test a failing transaction

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
91308e9bc88cb069fd1bcf88e910d042?d=identicon&s=25 Nick Snels (nicksnels)
on 2006-01-07 20:52
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
821395fe70906c8290df7f18ac4ac6cf?d=identicon&s=25 Rick Olson (Guest)
on 2006-01-07 21:40
(Received via mailing list)
On 1/7/06, Nick Snels <nick.snels@gmail.com> 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-...

--
rick
http://techno-weenie.net
This topic is locked and can not be replied to.