+ Start a Discussion
kristhoper saysonkristhoper sayson 

How to assert the return value

How can i assert for the return value of save(), Here is my code for class
public pagereference save() {

        ipAsset.AsLi_Status__c = AsLi_Constants.ASSETSTATUS_DRAFT;
        try {
            copyAccountCompanyDesc();
            upsert ipAsset;

            for(AsLi_AssetTechnology__c at : assetTechTags){
                if(at.AsLi_AssetMD__c == null){
                    at.AsLi_AssetMD__c = ipAsset.Id;
                }
            }

            upsert assetTechTags;
            
            
            PageReference IPAssetPage = new ApexPages.StandardController(ipasset).view();
                
            IPAssetPage.setRedirect(true);
            return IPAssetPage;
            
        } catch (DmlException ex) {
            ApexPages.addMessages(ex);
            return null;
        }
            
    }
Here is my test class
RecordType rt = [select id,Name from RecordType where SobjectType='AsLi_IPAsset__c' And Name = 'RFP Answer'];
        System.debug(rt);
        
        List<AsLi_TechnologiesTags__c> techTest = new List<AsLi_TechnologiesTags__c>();
        AsLi_TechnologiesTags__c tags1 = new AsLi_TechnologiesTags__c(Name = 'Google', AsLi_Active__c = True, AsLi_ForApproval__c = False);
        AsLi_TechnologiesTags__c tags2 = new AsLi_TechnologiesTags__c(Name = 'Google Talk', AsLi_Active__c = True, AsLi_ForApproval__c = False);
        AsLi_TechnologiesTags__c tags3 = new AsLi_TechnologiesTags__c(Name = 'Google Drive', AsLi_Active__c = True, AsLi_ForApproval__c = False);
        
        techTest.add(tags1);
        techTest.add(tags2);
        techTest.add(tags3);
        
        insert techTest;
        
        AsLi_IPAsset__c asset2 = new AsLi_IPAsset__c(RecordTypeId = rt.Id, Name = 'test2');
        insert asset2;
        PageReference pageRef = new PageReference('https://c.cs24.visual.force.com/apex/AsLi_AssetEdit/?Id=' + asset2.Id);



Test.setCurrentPage(pageRef);
        
        ApexPages.StandardController sCon = new ApexPages.StandardController(asset2);
        AsLi_AssetEditControllerExtension controller = new  AsLi_AssetEditControllerExtension(sCon);
        AsLi_AssetEditControllerExtension.TechnologyWrapper wr = new AsLi_AssetEditControllerExtension.TechnologyWrapper();
        controller.showPopup();
        Test.startTest();
        PageReference search1 = controller.showPopup();
        controller.searchText = 'Google';
        controller.search();
        controller.getOnFirstPage();
        controller.getOnLastPage();
        controller.add();   
        controller.save();
        Test.stopTest();
        
        
        
        System.assertEquals(3,controller.searchCount.size());
        AsLi_AssetTechnology__c assetTag = [Select Id, Name, AsLi_TechnologiesTagsMD__c, AsLi_AssetMD__c From AsLi_AssetTechnology__c];
        
        System.debug(assetTag);
        System.assertEquals(asset2.id,assetTag.AsLi_AssetMD__c);
        System.assertEquals(techTest[0].id,assetTag.AsLi_TechnologiesTagsMD__c);
        

        
    }
Vivek_PatelVivek_Patel
Hi Kristhoper

As your controller.save() methods return pagereference object.

you can call getRedirect() and getUrl() methods on the returned pagereference object and assert them with your expected results.

Regards,
Vivek Patel.

Please mark this as best answer if this solves you problem.
Mert YALTIMert YALTI

Hi Kristhoper;

Please try to implement the code given below;
 

String assertURL = controller.save().getUrl();

system.assertEquals('YOUR URL',assertURL);