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
Ramesh VaratharajRamesh Varatharaj 

Help with test class

i need help on test class for the below class (i took it from the internet and modified to fit my requirement). I am not from development backgroup, so yours help is much appreciated.

 

public class RSInsert{
Apexpages.StandardController controller;
public RSInsert(Apexpages.StandardController c){ 
    controller = c;
    }
public PageReference save() { 
controller.save();
 PageReference onn = new PageReference('https://cs30.salesforce.com/apex/FTESampleEdit?id='+controller.getId());
    Onn.setRedirect(true);    
    return onn;     //return Apexpages.currentPage(); 
    }
}

digamber.prasaddigamber.prasad

Hi,

 

I assume standardcontroller of your page is account and you can use below code snippet:-

 

@isTest
private class RSInsert_Test {
	
	public static testMethod void testRSInsert()
    {
		Account acct = new Account(Name = 'test name');
		insert acct;
		ApexPages.StandardController sc = new ApexPages.StandardController(acct);
        RSInsert testRSInsert = new RSInsert(sc);
		PageReference pageRef = Page.RSInsertPage;	//assuming name of page is RSInsertPage
		Test.setCurrentPage(pageRef);
		testRSInsert.save();
	}
}

 Anyway, you can modify it to solve your problem.

Let me know if you have any issue with it.

Happy to help you!

 

 

Ramesh VaratharajRamesh Varatharaj
thank you Prasad. the standard controller is for a custom object called Price_book__c. i do not have page called RSInsert Page, please let me know what should i replace with.

I have added lines to insert account, opportunity and the related pirce book entry.
digamber.prasaddigamber.prasad

Hi,

 

Please try below code snippet. I assumet only mandatory field on Price_Book__c is Name field:-

 

@isTest
private class RSInsert_Test {
	
	public static testMethod void testRSInsert()
    {
		Price_book__c priceBook = new Price_book__c(Name = 'test name');
		insert priceBook;
		ApexPages.StandardController sc = new ApexPages.StandardController(priceBook);
        RSInsert testRSInsert = new RSInsert(sc);
		//PageReference pageRef = Page.<your page name>;	//assuming name of page is RSInsertPage
		//Test.setCurrentPage(pageRef);
		testRSInsert.save();
	}
}

 Please let me know if you still see any issue.

 

Happy to help you!

 

 

Ramesh VaratharajRamesh Varatharaj

Prasad, i got it to work. Thanks for your help.