+ Start a Discussion
Sunil.VarmaSunil.Varma 

Got stucked in test class for my simple method

Apex Method:
public void cartss(){
       Products__c Pro = [Select Id, Name, Brand__c,Deal_Price__c,Model__c,Image__c from Products__c where id =:accId];
     
            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');
    }


Here is my Test class:


 private static testMethod void cartssTest() {
        Category__c cat = new Category__c();
        cat.Name='ss';
        insert cat;
        Sub_Category__c sub = new Sub_Category__c();
        sub.Name='sfdc';
        sub.Category__c=cat.Id;
        insert sub;
        Brands__c br = new Brands__c();
        br.Name='hell';
        br.Sub_Category__c=sub.Id;
        Products__c  testpr = new Products__c();
        testpr.name='testpr';
        testpr.Category__c=cat.Id;
        testpr.Sub_Category__c=sub.Id;
        testpr.Model__c='sfs';
        testpr.Deal_Price__c=8597;
        testpr.Image__c='heloo123';
        testpr.Brand__c=br.Id;
        insert testpr;        
      
        test.startTest();
        PageReference pageRef = Page.ezshopindex;
        SearchInVFController  cart = new SearchInVFController();
        cart.cartss();
        test.stopTest();
    }



Brands__c is lookup for products__c controlling field is Sub_Category__c, 
and Sub_category__c is lookup for products__c and controlling field is Category__c.
Category__c is lookup for Products__c
Best Answer chosen by Sunil.Varma
Sunil.VarmaSunil.Varma
Any help would be Appreciated!!!
 

All Answers

Sunil.VarmaSunil.Varma
Any help would be Appreciated!!!
 
This was selected as the best answer
Tad Aalgaard 3Tad Aalgaard 3
I don't see anywhere in your test class where you are setting accId.

Try this.
ApexPages.currentPage().getParameters().put('accId', testpr.Id); 
SearchInVFController  ctrl = new AddOpportunityController(); 
ctrl.cartss();