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
Sachin Singh 1Sachin Singh 1 

Test Class

I am noew to Salesforce and wanted to write the test class for the below code.......
Need help...

public class UnclaimedGiftsattachment
{
public Unclaimed_Gifts__c objcase{get;set;}
public Attachment myAttachment{get;set;}
public string fileName{get;set;} 
public Blob fileBody{get;set;}

    public UnclaimedGiftsattachment(Apexpages.standardcontroller controller)
    {
        objcase = new Unclaimed_Gifts__c();
        myAttachment =new Attachment();
    }
    public pagereference save()
    {
        insert objcase;
        System.debug('@@@@@fileBody'+fileBody);     
        myAttachment  = new Attachment();
              if (fileBody !=null){
                  Integer i=0;
                  myAttachment.clear();
                  myAttachment.Body = fileBody; 
                  myAttachment.Name = 'Logo_'+objcase.id+'.jpeg' ; 
                  myAttachment.ParentId = objcase.id;             
                  insert myAttachment;
              } 
        pagereference pr = new pagereference('/'+objcase.id);                           
        return pr;
    }
}
Sachin Singh 1Sachin Singh 1
I have started and my Test class is below....

@IsTest
private class TestUnclaimedGiftsattachment {

    static testMethod void MyTest(){
        //Inserting some test data.
        Unclaimed_Gifts__c  UGifts = new Unclaimed_Gifts__c( Amount__c=100);
        insert UGifts;    
        Attachment myAttach = new Attachment();
        myAttach.ParentId = UGifts.id;
        myAttach.name = 'Test File Name';
        myAttach.body = blob.valueOf('Test Attachement');
        insert myAttach;  
        
        PageReference pref = Page.NewUnclaimedGiftsPage;
        //Starting test
        Test.startTest();
        Unclaimed_Gifts__c  ObjTest = new Unclaimed_Gifts__c ();
        
        pref =  ObjTest.save();
        Test.stoptest();
    }
}


Its throwing me the error.....'Error: Compile Error: Method does not exist or incorrect signature: [Unclaimed_Gifts__c].save() at line 19 column 17'

I want this to resolve ASAP......Thanks in advance.
Himanshu ParasharHimanshu Parashar
Hi Sachin,

This code will work for you.
@IsTest
private class TestUnclaimedGiftsattachment {

    static testMethod void MyTest(){
       
         Unclaimed_Gifts__c  ObjTest = new Unclaimed_Gifts__c ();
        ApexPages.StandardController sc = new ApexPages.StandardController(ObjTest );
        UnclaimedGiftsattachment testController = new UnclaimedGiftsattachment(sc);
        //Starting test
        Test.startTest();
       UnclaimedGiftsattachment testController = new UnclaimedGiftsattachment(sc);
        pagereference pref =new pagerefrence();
        pref=  testController.save();
        Test.stoptest();
    }
}


 
Sachin Singh 1Sachin Singh 1
Hi Himanshu,

Thanks for the answer for this, It was really very helpful for me. I am working on some email services and need help again. Can u ple help me over this as well.

Link for my new quwestion is ...
https://developer.salesforce.com/forums/ForumsMain?id=906F0000000Az4IIAS