English 中文(简体)
ApexTrigger(在更新前插入)的测试案例
原标题:Test Case for Trigger(before insert, before update) in Apex

任何人都请帮助我写出测试案例,说明在ex顶的以下触发点的100%的编码覆盖率。

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;
}
}

• 部队; 测试显示100%的法典覆盖面

但如果是部队,则显示有6%的错误。

感谢任何帮助。

感谢。

最佳回答

引证:

@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;
    }
}

Also, make sure custom objects match in both your sandbox and deployment/production server.

问题回答

为了达到100%的编码覆盖率,你需要为每一份“如果”说明写一个测试。

目前,只有一次测试,一次测试是“如果是的话”。





相关问题
Salesforce - Populate text are from drop down selection

I have a picklist with three values and a text area that has three values as well, but I would like to correlate one value from the picklist with one value of the text area. Any thoughts as on how I ...

Setting Timeout value for Salesforce Web Service/API

The API for Salesforce is a web service, you set it up by downloading a WSDL file from Salesforce and adding the WSDL to your .NET project. But I can t find anywhere to set the Timeout value. ...

Salesforce Custom Objects

Hi I am trying to create a Custom Object in Salesforce.com Developer Edition, because I would then like to use this Custom Object in a New Custom Tab say "Properties". Properties tab will contain ...

热门标签