(我在这些代码示例中对公司的机密数据进行了模糊处理.嗨老板!)
这是测试:
require 'test_helper'class FoocorpCheckStatusWorkerTest < ActiveSupport::TestCase REQUEST_headerS = {'Accept'=>'*/*','Accept-EnCoding'=>'gzip;q=1.0,deflate;q=0.6,IDentity;q=0.3','Content-Type'=>'text/xml','User-Agent'=>'Ruby'} EXPECTED_BODY = "<?xml version=\"1.0\" enCoding=\"UTF-8\"?>\n<POSBizServices xmlns=\"http://API.foocorp.com\">\n <POSBizServiceheader>\n <userID>ABC</userID>\n <password>abc123</password>\n <clIEntID>123</clIEntID>\n <action>CHECKSTATUS</action>\n <responseFormat>XML</responseFormat>\n </POSBizServiceheader>\n <request>\n <checkStatus>\n <ecpdID>123</ecpdID>\n <refNum>12345678</refNum>\n </checkStatus>\n </request>\n</POSBizServices>\n" def sample_response_body(order_level_status_code: '',line_level_status_code: '') return <<-SAMPLE_RESPONSE_BODY <?xml version="1.0" enCoding="UTF-8"?> <POSBizServices xmlns="http://API.foocorp.com"> <POSBizServiceheader> <userID>ABC</userID> <password>abc123</password> <clIEntID>123</clIEntID> <statusCode>#{order_level_status_code}</statusCode> <statusDescription>2015-01-29 14:03:07 [QA13:MS_WSQA4_ONEPOS_01:01] : Request Processed Successfully</statusDescription> <action>CHECKSTATUS</action> <responseFormat>XML</responseFormat> </POSBizServiceheader> <response> <statusResponse> <count>1</count> <orderStatus> <orderID>12345678</orderID> <orderDate>01/27/2015</orderDate> <stats>1,0</stats> <aceOrderNo></aceOrderNo> <locationCode></locationCode> <lineInfoList> <count>1</count> <linestatus> <lineseq>1</lineseq> <statusCode>#{line_level_status_code}</statusCode> <statusDesc>No Status Yet.</statusDesc> </linestatus> </lineInfoList> </orderStatus> </statusResponse> </response> </POSBizServices> SAMPLE_RESPONSE_BODY end test "retry codes leave activity processor step in waiting state and activity in auto_processing state" do activity_processor_step = activity_processor_steps(:vip_vm_password_reset_step) activity = activity_processor_step.activity FoocorpVipCheckStatusWorker::STATUS_CODE_MATRIX[:retry].each do |status_code_pair| stub_request(:post,FOOCORP_VIP_CONfig[:base_url]). with(:body => /^.*$/,:headers => REQUEST_headerS ). to_return(:status => 200,:body => sample_response_body(order_level_status_code: status_code_pair.first,line_level_status_code: status_code_pair.last),:headers => {}) FoocorpVipCheckStatusWorker.new.perform activity.reload assert_equal("auto_processing",activity.state) activity_processor_step.reload assert_equal("waiting",activity_processor_step.state) end end test "success codes complete activity and activity processor step" do activity_processor_step = activity_processor_steps(:vip_vm_password_reset_step) activity = activity_processor_step.activity FoocorpVipCheckStatusWorker::STATUS_CODE_MATRIX[:success].each do |status_code_pair| stub_request(:post,:headers => {}) FoocorpVipCheckStatusWorker.new.perform activity.reload assert_equal("completed",activity.state) activity_processor_step.reload assert_equal("completed",activity_processor_step.state) end endend
这是工人:
class FoocorpVipCheckStatusWorker @queue = :short_running # Foocorp VIP returns an order-level status code and description # and a line-level status code and description. The comments describing # the status codes in STATUS_CODE_MATRIX are from Foocorp's Docs. # # Array format for status codes is: # ["(order-level status code)","(line-level status code)"] STATUS_CODE_MATRIX = { :success => [ ["00057","00051"] # Request Processed Successfully ],:retry => [ ["00057","00053"],# No Status Yet ["00057","00054"],# Order processed successfully but activation is pending. ["00099",""],# Unexpected error occurred during processing e.g. null pointer exception which causes thread to terminate. ["00059",# Server time out occurred. ["10009",# Back End call returned invalID agent data. ["10010",# Application fails to load ECPD profile details. ["00058","10013"],# Backend calls fails to load Billing information for account. It may be due to some internal issue. ["00058","10032"],# Exception occurred in Bulk Service write ["00058","10033"],# Error occurred in Bulk Service write for reassign ["00058","10040"],# Sub Account create Failed ["00058","10144"] # UNABLE TO RETRIEVE CREDIT informatION ] } def perform ActivityProcessorStep.where(state: "waiting").where("vip_ref_num IS NOT NulL").each do |activity_processor_step| clIEnt = Remote::ClIEnts::FoocorpVipClIEnt.new( :params => { :ecpd_ID => activity_processor_step.activity.carrIEr_account.parent_account.API_access_ID,:vip_ref_num => activity_processor_step.vip_ref_num } ) response = clIEnt.check_status #Todo: Remove deBUGging code #binding.pry order_status_code = response.first[:order_status_code] order_status_desc = response.first[:order_status_desc] line_status_code = response.last[:line_status_code] line_status_desc = response.last[:line_status_desc] status_codes = [order_status_code,line_status_code] response_message = line_status_desc.present? ? line_status_desc : order_status_desc response_message ||= "No status description given" success = STATUS_CODE_MATRIX[:success].detect{ |codes_array| codes_array == [order_status_code,line_status_code]}.present? retry_status_check = STATUS_CODE_MATRIX[:retry].detect{ |codes_array| codes_array == [order_status_code,line_status_code]}.present? stale_request = retry_status_check && Time.Now > (activity_processor_step.created_at + activity_processor_step.days_to_expire.days) if success activity_processor_step.update_activity_status('Complete',nil,response_message) elsif stale_request activity_processor_step.update_activity_status('Failure',"Activity processor step expired.") elsif !retry_status_check activity_processor_step.update_activity_status('Failure',response_message) end end endend
这是我的终端输出:
运行两个测试用例:
me@domo-kun ~/my-project (feature_foocorp_check_status_worker=)$be rake test TEST=test/workers/foocorp_check_status_worker_test.rb Run options: --seed 58882# Running tests:.FFabulous tests in 2.009658s,0.9952 tests/s,11.4447 assertions/s. 1) Failure:FoocorpCheckStatusWorkerTest#test_success_codes_complete_activity_and_activity_processor_step [/Users/steven/Development/my-company/my_company/test/workers/foocorp_check_status_worker_test.rb:86]:Expected: "completed" Actual: "auto_processing"2 tests,23 assertions,1 failures,0 errors,0 skips
评论第一个(“重试”)测试用例:
me@domo-kun ~/my-project (feature_foocorp_check_status_worker=)$be rake test TEST=test/workers/foocorp_check_status_worker_test.rb Run options: --seed 14937# Running tests:.Fabulous tests in 3.474386s,0.2878 tests/s,0.5756 assertions/s.1 tests,2 assertions,0 failures,0 skips
当我同时运行两个测试并在worker中使用binding.pry(这里注释掉)来检查“成功”测试用例(失败的那个)期间的响应值时,我得到以下两个结果.在这两种情况下,我都期望得到相同的结果(第二个).
运行两个测试用例:
[1] pry(#<VerizonVipCheckStatusWorker>)> response=> [{:order_status_code=>"00057",:order_status_desc=>"",:_summary=>{:order_status_code=>"00057",:order_status_desc=>""}},{:line_status_code=>"00053",:line_status_desc=>"No Status Yet.",:_summary=>{}}]
评论第一个(“重试”)测试用例:
[2] pry(#<VerizonVipCheckStatusWorker>)> response=> [{:order_status_code=>"00057",{:line_status_code=>"00051",:_summary=>{}}]
最后一个例子中的响应值是我在“重试”测试用例的第一次迭代期间所期望的,而不是在“成功”情况下.
我已经尝试将设置方法中的内容移动到您在此处看到的常量和sample_response_body方法中.我也试过添加这个拆解方法,但它没有帮助:
def teardown WebMock.reset!end
我正在从头开始编写工作者和测试,所以我完全有可能在某处出现愚蠢的语法/逻辑错误.我为可能是一个过于冗长的问题而道歉,但这确实让我撕裂了我的头发.非常感谢您的帮助.谢谢.
解决方法 原来我在适配器类中缓存了我的响应,所以它忽略了WebMock给出的响应. 总结以上是内存溢出为你收集整理的单元测试 – 一个测试用例似乎干扰了其他测试用例全部内容,希望文章能够帮你解决单元测试 – 一个测试用例似乎干扰了其他测试用例所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)