1

任何人都请帮我编写测试用例以实现 100% 代码覆盖率,以便在 apex 中跟踪触发器

trigger ETicketAirlineUrlTrigger on E_Ticket__c (before insert, before update) {

  for(E_Ticket__c etNew : Trigger.New)
  {
    if(etNew.Airline__c =='Virgin Australia')
    {
        etNew.Airlines_Url__c = '<a href="http://www.virginaustralia.com/Personal/Bookings/Managebookings/index.htm">Virgin Australia</a>';
    }
    else if(etNew.Airline__c =='Virgin Atlantic')
    {
        etNew.Airlines_Url__c = '<a href="https://www.virgin-atlantic.com/en/us/manageyourflights/updatebooking/index.jsp">Virgin Atlantic</a>';
    }
    else if(etNew.Airline__c =='Virgin America')
    {
        etNew.Airlines_Url__c = '<a href="http://www.virginamerica.com/view-itinerary.html">Virgin America</a>';
    }
    else if(etNew.Airline__c =='US AIR')
    {
        etNew.Airlines_Url__c = '<a href="http://reservations.usairways.com/Default.aspx">US AIR</a>';
    }
    else if(etNew.Airline__c =='United Airlines')
    {
        etNew.Airlines_Url__c = '<a href="http://www.united.com/page/genericpage/0,,52172,00.html?navSource=dropDown&linkTitle=itineraries">United Airlines</a>';
    }
    else if(etNew.Airline__c =='Turkish Air')
    {
        etNew.Airlines_Url__c = '<a href="http://www4.thy.com/mybookings/reservations.tk?target=view&lang=en">Turkish Air</a>';
    }
    else if(etNew.Airline__c =='Thai Air')
    {
        etNew.Airlines_Url__c = '<a href="http://www.thaiair.com/AWR_PREM/view-trip.jsp">Thai Air</a>';
    }
    else if(etNew.Airline__c =='Swiss Airlines')
    {
        etNew.Airlines_Url__c = '<a href="http://www.swiss.com/web/EN/Pages/index.aspx?Country=US">Swiss Airlines</a>';
    }
    else if(etNew.Airline__c =='Southwest Airlines')
    {
        etNew.Airlines_Url__c = '<a href="https://www.southwest.com/flight/lookup-air-reservation.html?int=GSUBNAV-AIR-RETRIEVE&forceNe...">Southwest Airlines</a>';
    }
    else if(etNew.Airline__c =='South African')
    {
        etNew.Airlines_Url__c = '<a href="https://www.flysaa.com/Journeys/reservation/searchpnr.action?nameSpace=reservation">South African</a>';
    }
    else if(etNew.Airline__c =='Singapore Airlines')
    {
        etNew.Airlines_Url__c = '<a href="https://www.singaporeair.com/manageBooking-flow.form?execution=e1s1">Singapore Airlines</a>';
    }
    else if(etNew.Airline__c =='QATAR AIRWAYS')
    {
        etNew.Airlines_Url__c = '<a href="http://www.qatarairways.com/us/en/homepage.page">QATAR AIRWAYS</a>';
    }
    else if(etNew.Airline__c =='Qantas Airlines')
    {
        etNew.Airlines_Url__c = '<a href="http://www.qantas.com.au/travel/airlines/your-booking/global/en">Qantas Airlines</a>';
    }
    else if(etNew.Airline__c =='Malaysia')
    {
        etNew.Airlines_Url__c = '<a href="https://book.malaysiaairlines.com/itd/itd/lang/en/reservations">Malaysia</a>';
    }
    else if(etNew.Airline__c =='Lufthansa')
    {
        etNew.Airlines_Url__c = '<a href="http://www.lufthansa.com/online/portal/lh/us/my_account/my_bookings">Lufthansa</a>';
    }
    else if(etNew.Airline__c =='KLM - Royal Dutch Airlines')
    {
        etNew.Airlines_Url__c = '<a href="http://www.klm.com/travel/us_en/index.htm#tab=db_mmb">KLM - Royal Dutch Airlines</a>';
    }
    else if(etNew.Airline__c =='Japan Air')
    {
        etNew.Airlines_Url__c = '<a href="http://www.5971.jal.co.jp/eng/SearchReservation.do">Japan Air</a>';
    }
    else if(etNew.Airline__c =='Etihad Airlines')
    {
        etNew.Airlines_Url__c = '<a href="http://www.etihadairways.com/sites/etihad/_layouts/etihad/obe/RetrieveBooking.aspx">Etihad Airlines</a>';
    }
    else if(etNew.Airline__c =='Emirates')
    {
        etNew.Airlines_Url__c = '<a href="https://fly.emirates.com/MYB/MMBLogin.aspx">Emirates</a>';
    }
    else if(etNew.Airline__c =='Delta Airlines')
    {
        etNew.Airlines_Url__c = '<a href="http://www.delta.com/">Delta Airlines</a>';
    }
    else if(etNew.Airline__c =='Copa Airlines')
    {
        etNew.Airlines_Url__c = '<a href="http://www.copaair.com/sites/US/EN/Pages/homepage.aspx">Copa Airlines</a>';
    }
    else if(etNew.Airline__c =='Continental Airlines')
    {
        etNew.Airlines_Url__c = '<a href="http://www.continental.com/web/en-US/apps/reservation/default.aspx">Continental Airlines</a>';
    }
    else if(etNew.Airline__c =='Cathay Pacific')
    {
        etNew.Airlines_Url__c = '<a href="http://www.cathaypacific.com/cpa/en_US/manageyourtrip/managemybooking?loginType=nonmember">Cathay Pacific</a>';
    }
    else if(etNew.Airline__c =='British Airways')
    {
        etNew.Airlines_Url__c = '<a href="http://www.britishairways.com/travel/home/public/en_us">British Airways</a>';
    }
    else if(etNew.Airline__c =='American Airlines')
    {
        etNew.Airlines_Url__c = '<a href="https://www.aa.com/reservation/findReservationAccess.do">American Airlines</a>';
    }
    else if(etNew.Airline__c =='All Nippon')
    {
        etNew.Airlines_Url__c = '<a href="https://aswbe-i.ana.co.jp/p_per/sky_ip_per_jp/preReConfirmSearchPnr.do?CONNECTION_KIND=LAX&LA...">All Nippon</a>';
    }
    else if(etNew.Airline__c =='Alitalia')
    {
        etNew.Airlines_Url__c = '<a href="http://www.alitalia.com/US_EN/home/index.aspx">Alitalia</a>';
    }
    else if(etNew.Airline__c =='Alaska Airlines')
    {
        etNew.Airlines_Url__c = '<a href="https://www.alaskaair.com/booking/ssl/saved/ViewPnr.aspx">Alaska Airlines</a>';
    }
    else if(etNew.Airline__c =='Air New Zealand')
    {
        etNew.Airlines_Url__c = '<a href="https://flightbookings.airnewzealand.com/isbook_en_US/review/initNewWebCheckIn.do?workflowAction=...">Air New Zealand</a>';
    }
    else if(etNew.Airline__c =='Air France')
    {
        etNew.Airlines_Url__c = '<a href="https://www.airfrance.us/cgi-bin/AF/US/en/local/process/standard/rebooking/SearchPnrRbkAction.do?">Air France</a>';
    }
    else if(etNew.Airline__c =='Air Canada')
    {
        etNew.Airlines_Url__c = '<a href="http://www.aircanada.com/aco/manageMyBookings.do">Air Canada</a>';
    }
  }
}

并且无法使用以下测试用例获得 100% 的代码覆盖率

@isTest
private class ETicketAirlineUrlTestCase {

static testMethod void myUnitTest() {
    Opportunity oppNew =  new Opportunity();
    oppNew.Name = 'Test Opp';
    oppNew.StageName = 'To Be Searched';
    oppNew.CloseDate = System.now().date();
    insert oppNew;

    E_Ticket__c et =  new E_Ticket__c();
    et.Airline__c = 'Virgin Australia';
    et.Opportunity__c = oppNew.Id;
     et.Airline__c = 'Virgin Atlantic';
    insert et;

    E_Ticket__c etNew = [select Id, Airline__c from E_Ticket__c where Id = :et.Id];
    update etNew;
}
}

当我运行 Class-->Force.com-->Run Test 它显示 100% 的代码覆盖率

但是当我执行 Force.com--> 部署到服务器时,它显示错误 6% 已覆盖

任何帮助,将不胜感激

谢谢..

4

2 回答 2

3

为了获得 100% 的代码覆盖率,您需要为每个“else if”语句编写一个测试。

您目前只有一项测试,针对一项“else if”。

于 2012-04-09T19:58:32.933 回答
3

试试这个:

@isTest
private class ETicketAirlineUrlTestCase {

    static testMethod void myUnitTest() {
        Opportunity oppNew =  new Opportunity();
        oppNew.Name = 'Test Opp';
        oppNew.StageName = 'To Be Searched';
        oppNew.CloseDate = System.now().date();
        insert oppNew;

        List<E_Ticket__c> tickets = new List<E_Ticket__c>();

        E_Ticket__c et1 =  new E_Ticket__c();
        et1.Airline__c = 'Virgin Australia';
        et1.Opportunity__c = oppNew.Id;
        tickets.add(et1);

        E_Ticket__c et2 =  new E_Ticket__c();
        et2.Airline__c = 'Virgin Atlantic';
        et2.Opportunity__c = oppNew.Id;
        tickets.add(et2);

        // ...
        // add as many tickets needed for all Airlines
        // ...

        insert tickets;
        update tickets;
    }
}

此外,请确保自定义对象在您的沙盒和部署/生产服务器中都匹配。

于 2012-04-09T21:16:16.573 回答