所有人。
我要测试活动记录对象销毁失败,但是在创建失败情况时遇到问题。
我有一个名为 require_user_payment_info 的 before_filter 方法,该方法在删除方法之前先验证 @payment_info 对象被调用,因此在调用 delete 方法之前,我无法创建“错误”的 @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
有什么想法吗?
答案 0 :(得分:1)
这是我的解决方案:
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