function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Sunil.VarmaSunil.Varma 

Test class for my apex class

public pagereference payOrder(){
        List<Cart__c> cartList = [Select Id, Name__c, Brands__c,Price__c,Model__c,Image__c from Cart__c];
        List<Order__c> oList = New List<Order__c>();
        for(Cart__c cartOrder : cartList){
            Order__c oOrder = New Order__c();
            oOrder.Name__c =  cartOrder.Name__c;
            oOrder.Model__c =  cartOrder.Model__c;
            oOrder.Brand__c =  cartOrder.Brands__c;
            oOrder.Image__c =  cartOrder.Image__c;
            oOrder.Price__c =  cartOrder.Price__c;
            oList.add(oOrder);
        } 
        Insert oList;
        delete cartList;
        pagereference redirect = new PageReference('/apex/Redirect');
        return redirect;
    }

Can anyone help me out by writing test class for this.Thankyou!!
Best Answer chosen by Sunil.Varma
Khan AnasKhan Anas (Salesforce Developers) 
Hi Sunil,

Greetings to you!

Please try the below code, I have tested in my org and it is working fine. Kindly modify the code as per your requirement.
@isTest
public class Test_PayOrder {
    
    static testMethod void test1() {
        
        Cart__c ac = new Cart__c();
        // add fields as per your requirement and field type
        ac.Name__c = 'N_Test';
        ac.Brands__c = 'B_Test';
        ac.Price__c = '100';
        ac.Model__c = 'M_Model';
        ac.Image__c = 'I_Test';
        INSERT ac;
        
        Test.startTest();
          ClassName obj = new ClassName();
          obj.payOrder();
        Test.stopTest();
    }
}

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas

All Answers

Khan AnasKhan Anas (Salesforce Developers) 
Hi Sunil,

Greetings to you!

Please try the below code, I have tested in my org and it is working fine. Kindly modify the code as per your requirement.
@isTest
public class Test_PayOrder {
    
    static testMethod void test1() {
        
        Cart__c ac = new Cart__c();
        // add fields as per your requirement and field type
        ac.Name__c = 'N_Test';
        ac.Brands__c = 'B_Test';
        ac.Price__c = '100';
        ac.Model__c = 'M_Model';
        ac.Image__c = 'I_Test';
        INSERT ac;
        
        Test.startTest();
          ClassName obj = new ClassName();
          obj.payOrder();
        Test.stopTest();
    }
}

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas
This was selected as the best answer
Sunil.VarmaSunil.Varma
Hi Khan Anas,
You helped me and It worked. Thank you.
Sunil.VarmaSunil.Varma
Hi Khan Anas, 
Could you please help me with this method also:
Apex Class:
 
public void cartss(){
      
   Pro = [Select Id, Name, Brand__c,Deal_Price__c,Model__c,Image__c from Products__c where id =:accId];
// accId is Id of the product from the list of products.
           Cart__c cartOrder = New Cart__c();
            cartOrder.Name__c =  pro.Name;
            cartOrder.Model__c =  pro.Model__c;
            cartOrder.Brands__c =  pro.Brand__c;
            cartOrder.Image__c =  pro.Image__c;
            cartOrder.Price__c =  pro.Deal_Price__c;
          
        Insert cartOrder;
        pagereference redirect = new PageReference('/apex/ezshopindex');
    }

Thanking You in anticiption!!!