Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problems testing a destroy failure

Status
Not open for further replies.

c73mr0ck

Programmer
Oct 25, 2012
11
0
0
US
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:

Code:
  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


And the actual delete method:

Code:
  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

Any ideas?

Thanks!
 
Bingo - here's my solution:

Code:
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

Thanks for your help!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top