繁体   English   中英

如何测试红宝石在轨道上的破坏

[英]How to test destroy failure in ruby-on-rails

大家。

我要测试活动记录对象销毁失败,但是在创建失败情况时遇到了问题。

我有一个名为“require_user_payment_info”这验证就是所谓的删除方法之前@payment_info对象的before_filter方法,所以我不能@payment_info对象创建一个“坏”被称为删除方法之前。

这是require_user_payment_info方法:

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

和实际的删除方法:

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

有任何想法吗?

这是我的解决方案:

def test_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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM