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
Naveen Kumar Reddy YerramNaveen Kumar Reddy Yerram 

Variable does not exist: TestFactory at line 8 column 19

@isTest
private class AccountWrapper_Tests {
  @testSetup
  static void loadTestData(){
    List<Account> accounts = (List<Account>) Test.loadData(Account.SObjectType, 'accountData');
    List<Opportunity> opps = new List<Opportunity>();
    for(Account a : accounts){
      opps.addAll(TestFactory.generateOppsForAccount(a.id, 1000.00, 5));
// anyone tell me where to see for that variable wether it is there are not?
    } 
    insert opps;
  }

  @isTest static void testPositiveRoundedAveragePrice() {
    List<AccountWrapper> accounts = new List<AccountWrapper>();
    for(Account a : [SELECT ID, Name FROM ACCOUNT]){
      accounts.add(new AccountWrapper(a));
    }
    // sanity check asserting that we have opportunities before executing our tested method.
    List<Opportunity> sanityCheckListOfOpps = [SELECT ID FROM Opportunity];
    System.assert(sanityCheckListOfOpps.size() > 0, 'You need an opportunity to continue');
    Test.startTest();
    for(AccountWrapper a : accounts){
      System.assertEquals(a.getRoundedAvgPriceOfOpps(), 1000.00, 'Expected to get 1000.00');
    }
    Test.stopTest();
  }
    

      @isTest static void testHighPriority() {
        List<AccountWrapper> accounts = new List<AccountWrapper>();
        for(Account a : [SELECT ID, Name FROM ACCOUNT]){
            accounts.add(new AccountWrapper(a));}

          
            List<Opportunity> opps = new List<Opportunity>();
            opps = [SELECT Id,Amount FROM Opportunity];
              for(Opportunity opp :opps ) {
                  opp.amount =2000000;
              }
          update opps;
    
    Test.startTest();
      for(AccountWrapper a : accounts){
      System.assertEquals(a.isHighPriority(), true, 'Priority expected to be high');
      System.debug('get rounded price of opps for accounts b:'+ a.getRoundedAvgPriceOfOpps());
    }
    Test.stopTest();
  }
}
Best Answer chosen by Naveen Kumar Reddy Yerram
Ashish Singh SFDCAshish Singh SFDC
Hi Naveen,

It seems like the Class TestFactory is not available in one of your environment. Can you try ctrl+shift+O and in the popup type *TestFactory*. If you're able to get the result with the name TestFactory.apxc then open the class and then look for the method generateOppsForAccount and see what that method is returning.

If you're unable to find the class then you need to search same class in your other environment including Production. If it's not then you need to create a new class with the method from which you can generate your test Opportunity record.

You can also try to search in files by Ctrl+shift+h and in pop up type *TestFactory* and see if you can find anything related to TestFactory. 

*TestSuiteMembership.obj* => This is basically Object defination and not the class.

Thanks,
Ashish Singh.

All Answers

mukesh guptamukesh gupta
Hi Naveen, 

Please opne TestFactory class and share the line 8 code with me , or you can share TestFactory.generateOppsForAccount  code for me for investigation 

REgards
Mukesh
PriyaPriya (Salesforce Developers) 

Hi Naveen,

Please try with "TestDataFactory" instead of "TestFactory".

If it resolve, please mark it as best answer.

regards,

Priya Ranjan

Naveen Kumar Reddy YerramNaveen Kumar Reddy Yerram
getting error variable does not exist : TestDataFactory
Naveen Kumar Reddy YerramNaveen Kumar Reddy Yerram
i don't have the *TestFactory class *but i have a class named as *TestSuiteMembership.obj*
Ashish Singh SFDCAshish Singh SFDC
Hi Naveen,

It seems like the Class TestFactory is not available in one of your environment. Can you try ctrl+shift+O and in the popup type *TestFactory*. If you're able to get the result with the name TestFactory.apxc then open the class and then look for the method generateOppsForAccount and see what that method is returning.

If you're unable to find the class then you need to search same class in your other environment including Production. If it's not then you need to create a new class with the method from which you can generate your test Opportunity record.

You can also try to search in files by Ctrl+shift+h and in pop up type *TestFactory* and see if you can find anything related to TestFactory. 

*TestSuiteMembership.obj* => This is basically Object defination and not the class.

Thanks,
Ashish Singh.
This was selected as the best answer
Naveen Kumar Reddy YerramNaveen Kumar Reddy Yerram
Ok sir/ma'am thank you
Mahesh NagarwalMahesh Nagarwal

TestFactory is a custom class which was created into this step - 

https://trailhead.salesforce.com/content/learn/modules/unit-testing-on-the-lightning-platform/generate-data-for-tests

Or just follow this Solution - 

1. Create a Apex class TestFactory
2. Copy this code 

@isTest
public class TestFactory {
  public static Account getAccount(String name, Boolean doInsert){
    Account a = new Account(name = name);
    if(doInsert){
      insert a;
    }
    return a;
  }
  public static Contact getContact(Id accountId, String fname, String lname, Boolean doInsert){
    Contact c = new Contact(firstName = fname, lastName = lname, accountId = accountId);
    if(doInsert){
      insert c;
    }
    return c;
  }
  public static void generateAccountWithContacts(Integer numContacts){
    Account a = getAccount('default account ltd', true);
    List<Contact> contacts = new List<Contact>();
    for(Integer i = 0; i < numContacts; i++){
      String contactName = 'contact' + i;
      contacts.add(getContact(a.id, contactName, contactName, false));
    }
    insert contacts;
  }
  public static Opportunity[] generateOppsForAccount(id accountId, Decimal amount, Integer numOpps){
    List<Opportunity> opps = new List<Opportunity>();
    for(Integer i = 0; i < numOpps; i++){
      Opportunity o = new Opportunity();
      o.name = 'Account ' + i;
      o.accountId = accountid;
      o.amount = amount;
      o.closeDate = Date.today().addDays(5);
      o.stageName = 'Prospecting';
      opps.add(o);
    }
    return opps;
  }
  public static User generateUser(String profileName){
    UserRole userRole = new UserRole(DeveloperName = 'TestingTeam', Name = 'Testing Team');
    insert userRole;
    User u = new User(
      ProfileId = [SELECT Id FROM Profile WHERE Name = :profileName].Id,
      LastName = 'last',
      Email = 'Cpt.Awesome@awesomesauce.com',
      Username = 'Cpt.Awesome@awesomesauce.com',
      CompanyName = 'Testing Co',
      Title = 'Captian',
      Alias = 'alias',
      TimeZoneSidKey = 'America/Los_Angeles',
      EmailEncodingKey = 'UTF-8',
      LanguageLocaleKey = 'en_US',
      LocaleSidKey = 'en_US',
      UserRoleId = userRole.Id
    );
    insert u;
    return u;
  }
}

3. Select this as best answer.