我目前遇到了一个循环,其中retry在我的测试中被执行。我怎样才能stubretry并仍然测试它上面的代码?我有一段代码可以捕获HostKeyMismatch异常:rescueNet::SSH::HostKeyMismatch=>ee.remember_host!retryend我的规范:describe"rescuesNet::SSH::HostKeyMismatch"doit"resyncsthesshkeys"doNet::SSH::HostKeyMismatch.any_instance.should_receive(:remember_host!).and_return(tr
我有一个这样的循环:#Iteratealistofitemsreq_wf_list.eachdo|req_wf|#BeginatransactionReqWf.transactiondo#ReqWfisanActiveRecordmodelclass#Dosomethings#...#1.Iwanttobeabletocontinueprocessingwiththe#nextiterationoftheloopifthereisanerrorhere#2.Ialsowanttorollbackthetransactionassociatedwith#thisparticularite
我有一个简单的异常处理blockbeginrescueend我想修改它,以便在救援时重新开始。尝试5次。如果仍然没有连接-从街区出来 最佳答案 您需要关键字retry。将其与MAX_RETRIES结合起来。即if(MAX_RETRIES-=1)>0重试 关于ruby-on-rails-Ruby中的异常处理-如果需要救援,则调用begin,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/question
我在任何Ruby对象中都找不到rescue。我可以通过以下方式找到raise:Kernel.private_instance_methods.grep(/^rai/)但是,救援在哪里? 最佳答案 rescue不是一种方法。它是一个关键字,硬连接到Ruby中。 关于ruby-救援方法在哪里?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/21959462/
我有这样的方法defclassNamedefmethod_namesomecoderescuesomecodeanderrormessageendend那么,如何写下rspec来测试救援block..? 最佳答案 如果你想拯救,这意味着你希望一些代码引发某种异常。您可以使用RSpecstubs伪造实现并强制出错。假设执行block包含一个可能引发的方法defmethod_nameother_method_that_may_raiserescue=>e"ERROR:#{e.message}"end在您的规范中将stub挂接到该方法it
我使用stripe作为支付处理器。在应用程序中,我向Stripe发送请求以执行收费或其他类型的进程,并且基本上使用与下面相同的错误处理样板。rescueStripe::InvalidRequestError=>e,#dosomethingrescueStripe::AuthenticationError=>e,#dosomethingrescueStripe::APIConnectionError=>e,#dosomethingrescueStripe::StripeError=>e#dosomethingrescue=>e#dosomethingend虽然我绝对可以在每个API调用中
我有一些看起来与此类似的代码:foo=SomeActiveRecordModel.where(bar:10).first.foorescue''一旦我开始使用Rubocop,它就对我大喊大叫,要求我使用救援语法。所以我认为至少还有两种其他方法可以编写此代码:foo=beginfoo=SomeActiveRecordModel.where(bar:10).first.foorescueNoMethodError''end和:foo=SomeActiveRecordModel.where(bar:10).firstfoo.present??foo.foo:''首选这些方法中的哪一种,或者是
我有以下代码行出错:rescueTimeout::Error=>elogs.puts("Rescuedatimeouterror...#{e}")email_ids_all.eachdo|email_delete|call="/api/v2/emails/#{email_delete}/"uri=HTTParty.delete("https://www.surveys.com#{call}",:basic_auth=>auth,:headers=>{'ContentType'=>'application/x-www-form-urlencoded','Content-Length'=>
问题很简单:像这样使用rescue是个好习惯吗?......如果不是,解决方案在哪里?一个“如果”之前? 最佳答案 使用trymethod在Rails中:在Ruby2.3+中你可以使用原生的safenavigationoperator: 关于ruby-on-rails-在erb模板中进行救援的好做法?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/24692565/
Rubyrescue语句修饰符是否与require一起使用?irb(main):001:0>require'a'rescuenilLoadError:nosuchfiletoload--afrom(irb):1:in`require'from(irb):1from:0 最佳答案 你可以从LoadError中拯救你只需要使用begin/end风格而不是使用内联rescue:这如您所愿:beginrequire'a'rescueLoadError=>exputs"Loaderror:#{ex.message}"end