• baboonworksfine
  • NEWBIE
  • 0 Points
  • Member since 2012

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 2
    Replies

I have following test method which set up Opportunity OpportunityLineItem objects, but when I try to retrieve the OpportunityLineItems from opportunity, I get nothing returned.

 

@isTest(SeeAllData=true)
public class QuickEditTest {
    
    static testMethod void opportunityEditTest() {
        
        //System.debug([SELECT id FROM Pricebook2 WHERE isStandard=true].Id);
    
        // setting up data
        
        Product2 pd = new Product2(Name = 'Test product', IsActive = true);
        insert pd;
        
        Pricebook2 pb = new Pricebook2(Name = 'Test Pricebook', IsActive = true);                
        insert pb;
        
        Pricebook2 stdpb = [select id from Pricebook2 where isStandard=true];

        PricebookEntry pbe = new PricebookEntry(
            Pricebook2Id = stdpb.Id,
            Product2Id = pd.Id,
            UnitPrice = 100.00,
            IsActive = true,
            UseStandardPrice = false
        );
        insert pbe;
        
        pbe = new PricebookEntry(
            Pricebook2Id = pb.Id,
            Product2Id = pd.Id,
            UnitPrice = 100.00,
            IsActive = true
        );
        insert pbe;        
        
        // Opportunity & OpportunityLineItem
        Opportunity op = new Opportunity(
            Name = 'TestOpportunity1', 
            CloseDate = System.today(), 
            StageName = 'Prospecting'
        );
        insert op;
        
        OpportunityLineItem ol = new OpportunityLineItem(
            PricebookEntryId = pbe.Id, 
            OpportunityId = op.Id, 
            Quantity = 2,
            TotalPrice = 2 * pbe.UnitPrice
        );                
        insert ol;
        System.debug(op);
        insert ol;

       
        System.debug(op.OpportunityLineItem); /* this returns nothing! */ 
    }
}

 

System.debug(op.OpportunityLineItem) print nothing! Why?

Suppose I have a VF page called "Quick_Edit" for updating an opportunity:

<apex:page standardController="Opportunity">
  <apex:pageMessages />

  <apex:form >
    <apex:pageBlock title="Quick Edit">

      <apex:pageBlockButtons style="float:right;">
        <apex:commandButton value="Save" action="{!save}"/>
        <apex:commandButton value="Cancel" action="{!cancel}"/>
      </apex:pageBlockButtons>
                        
      <span>Opportunity Name:</span>
      <apex:inputField value="{!opportunity.Name}"/>

    </apex:pageBlock>
  </apex:form>

</apex:page>

 

I have a test class like this:

@isTest
public class QuickEditTest {
    
    static testMethod void opportunityEditTest() {
        
        // setting up data
        
        Opportunity op = new Opportunity(
            Name = 'TestOpportunity1', 
            CloseDate = System.today(), 
            StageName = 'Prospecting'
        );
        
        insert op;
           
        
        PageReference pageRef = new PageReference('/apex/'+op.Id);
        Test.setCurrentPage(pageRef);
        
        // now what should I do
    }
}

1. Is it possible to insert data into the form and test the save action?

2. If not, do I have to created getters/setters in a controller (say opportunity extension controller) to set updated field value directly on the opportunity object and then call save?

3. If there is no real good way to interacted with the VF page in tests, what is the purpors of using Test.setCurrentPage(pageRef)? Because I can just test the controller alone.

 

If anybody develops Rails app, you probably know what I need is functionalities like Capybara or Webrat ... 

I have following test method which set up Opportunity OpportunityLineItem objects, but when I try to retrieve the OpportunityLineItems from opportunity, I get nothing returned.

 

@isTest(SeeAllData=true)
public class QuickEditTest {
    
    static testMethod void opportunityEditTest() {
        
        //System.debug([SELECT id FROM Pricebook2 WHERE isStandard=true].Id);
    
        // setting up data
        
        Product2 pd = new Product2(Name = 'Test product', IsActive = true);
        insert pd;
        
        Pricebook2 pb = new Pricebook2(Name = 'Test Pricebook', IsActive = true);                
        insert pb;
        
        Pricebook2 stdpb = [select id from Pricebook2 where isStandard=true];

        PricebookEntry pbe = new PricebookEntry(
            Pricebook2Id = stdpb.Id,
            Product2Id = pd.Id,
            UnitPrice = 100.00,
            IsActive = true,
            UseStandardPrice = false
        );
        insert pbe;
        
        pbe = new PricebookEntry(
            Pricebook2Id = pb.Id,
            Product2Id = pd.Id,
            UnitPrice = 100.00,
            IsActive = true
        );
        insert pbe;        
        
        // Opportunity & OpportunityLineItem
        Opportunity op = new Opportunity(
            Name = 'TestOpportunity1', 
            CloseDate = System.today(), 
            StageName = 'Prospecting'
        );
        insert op;
        
        OpportunityLineItem ol = new OpportunityLineItem(
            PricebookEntryId = pbe.Id, 
            OpportunityId = op.Id, 
            Quantity = 2,
            TotalPrice = 2 * pbe.UnitPrice
        );                
        insert ol;
        System.debug(op);
        insert ol;

       
        System.debug(op.OpportunityLineItem); /* this returns nothing! */ 
    }
}

 

System.debug(op.OpportunityLineItem) print nothing! Why?