Solved

Problems testing a destroy failure

Posted on 2014-01-05
1
178 Views
Last Modified: 2014-01-07
Hello,
   I'm trying to test an active record object destroy failure but I'm having problems creating a failure situation.  I have a before_filter method called 'require_user_payment_info' which validates the @payment_info object before the delete method is called so I can't create a 'bad' @payment_info object before the delete method is called.

Here's the require_user_payment_info method:

  def require_user_payment_info 
    @payment_info = credit_card_model.slave.find_by_user_guid(user_guid)
    if !@payment_info || @payment_info.user_guid != user_guid
      redirect_to(:controller => 'store', :action => 'index') and return false
    else
      if((@payment_info.card_expires_year.to_i < Date.today.year) || 
          ((@payment_info.card_expires_month.to_i < Date.today.month) && (@payment_info.card_expires_year.to_i == Date.today.year)))        
        @payment_info.card_account_public = "" #clear this out so the user is forced to re-enter the credit card number
        @payment_info.valid?        
        flash.now[:error] = t('ui_flash_errors.card_expired_error')
      end
    end
  end

Open in new window



And the actual delete method:

  def delete
    # required to be called via a delete request
    redirect_to :action => 'edit' and return if !request.delete?
    if @payment_info.destroy
      flash[:notice] = "Delete SUCCESSFUL"
      redirect_to :action => 'new'
    else
      flash[:error] = "Delete failed"
      redirect_to :action => 'edit'
    end

Open in new window


Any ideas?

Thanks!
0
Comment
Question by:clemcrock
1 Comment
 

Accepted Solution

by:
clemcrock earned 0 total points
ID: 39763984
Bingo - here's my solution:

def test_unsuccessful_delete
    payment_info = Factory.create(:payment_info, :user_guid=>@user.guid, :card_expires_month=>'04', 
                                    :card_expires_year=>(Date.today.year+2).to_s, :cardholder_city=>"test city",
                                      :cardholder_state=>'NC', :cardholder_country=>'US', :cardholder_zip=>'27612')
    PaymentInfo.any_instance.stubs(:destroy).returns(false)
    
    delete(:delete, {}, @session)
    assert_response(:redirect)
    assert_equal false, assigns(:payment_info).blank?
    assert_redirected_to({:controller=>'account', :action=>'edit'})
    assert_equal flash[:error], "There was an error deleting your credit card information. Please try again."
  end

Open in new window


Thanks for your help!
0

Featured Post

ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Cannot get progress bars to work w/ Apache Progress Module 3 416
Add a simple drop down from controller 5 381
cucumber related issue 8 398
return files 6 108
In Ruby, Call or invoke a API DLL library is easily via Win32API class, win32-api gem or other gems. For general DLL API call, there are quite a few references, some good tips list below: http://www.rubytips.org/2008/05/13/accessing-windows-api-fro…
Recently I spent hours debugging an issue in a Rails project where ActiveRecord was causing MySQL errors trying to create a User object of a class at the top level of a Single Table Inheritance model structure.  It turns out `.create` behaves differ…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question