@isTest
private class ConsumApplySplitControllerTest {
    static ConsumTestDataFactory factory;
    // 造 两个一览，各一条明细，草案中
    @testSetup static void setupTestData(){
        ConsumTestDataFactory factory = new ConsumTestDataFactory();
        factory.setupTestData();
        factory.selectDetails(2);
        TestDataUtility.CreatePIPolicyConfiguration();
    }
    // 䓍案中分单
    static testMethod void testSaveSplitError() {
        Consum_Apply__c ca = [SELECT Id FROM Consum_Apply__c];
        List<Consum_Apply_Equipment_Set__c> caesList = [SELECT Id FROM Consum_Apply_Equipment_Set__c WHERE Consum_Apply__c=:ca.Id];
        System.assertEquals(2, caesList .size());

        PageReference ref = new PageReference('/apex/ConsumApplySplit');
        ref.getParameters().put('objId', ca.Id);
        ref.getParameters().put('raesIds', caesList[0].Id);
        Test.setCurrentPage(ref);
        ConsumApplySplitController controller = new ConsumApplySplitController();
        controller.init();

        Test.startTest();
        controller.cloneRas.Split_Apply_Reason__c = '分批发货分单';
        controller.saveSplit();
        Test.stopTest();

        // 分单失败，只有一个申请
        List<Consum_Apply__c> caList = [SELECT Id FROM Consum_Apply__c];
        System.assertEquals(1, caList.size());
    }

    // 批准后分单
    static testMethod void testSaveSplit() {
        // 变成已批准
        ConsumTestDataFactory factory = new ConsumTestDataFactory();
        factory.approve();

        List<Consum_Apply_Equipment_Set__c> caesList = [SELECT Id FROM Consum_Apply_Equipment_Set__c WHERE Consum_Apply__c=:factory.ca.Id];
        System.assertEquals(2, caesList .size());

        PageReference ref = new PageReference('/apex/ConsumApplySplit');
        ref.getParameters().put('objId', factory.ca.Id);
        ref.getParameters().put('raesIds', caesList[0].Id);
        Test.setCurrentPage(ref);
        ConsumApplySplitController controller = new ConsumApplySplitController();
        controller.init();

        Test.startTest();
        controller.cloneRas.Split_Apply_Reason__c = '分批发货分单';
        controller.saveSplit();
        Test.stopTest();

        // 分单成功，有两个申请
        List<Consum_Apply__c> caList = [SELECT Id FROM Consum_Apply__c];
        System.assertEquals(2, caList.size());
    }
}