+ Start a Discussion
John GuerriereJohn Guerriere 

One method in my Apex Test is Failing....

Hi there,

Below is my test class where the SearchMembers method fails: System.AssertException: Assertion Failed: Expected: 4, Actual: 0

The testLoadData method passes....

Any help appreciated....
==
@isTest
private class MembershipDirectoryControllerTest {

    @testSetup static void createData() {
        List<Account> accounts = new List<Account>();
        Account a1 = new Account();
        a1.Name = 'Test 1';
        a1.Membership_Level__c = 'Catalyst';
        a1.Category__c = 'Farms';
        a1.Keywords__c = 'farm';
        a1.BillingPostalCode = '11111';
        a1.Directory_Display_Override__c = true;
        accounts.add(a1);

        Account a2 = new Account();
        a2.Name = 'Test 2';
        a2.Membership_Level__c = 'Champion';
        a2.Category__c = 'Education';
        a2.Keywords__c = 'test';
        a2.BillingPostalCode = '22222';
        a2.Directory_Display_Override__c = true;
        accounts.add(a2);

        Account a3 = new Account();
        a3.Name = 'Test 3';
        a3.Membership_Level__c = 'Catalyst';
        a3.Category__c = 'Farms';
        a3.Keywords__c = 'farm';
        a3.BillingPostalCode = '11111';
        a3.Directory_Display_Override__c = true;
        accounts.add(a3);

        Account a4 = new Account();
        a4.Name = 'Test 4';
        a4.Membership_Level__c = 'Champion';
        a4.Category__c = 'Business Supplies';
        a4.Keywords__c = 'farm';
        a4.BillingPostalCode = '22222';
        a4.Directory_Display_Override__c = true;
        accounts.add(a4);

        Account a5 = new Account();
        a5.Name = 'Test 5';
        a5.Membership_Level__c = 'Catalyst';
        a5.Category__c = 'Farms';
        a5.Keywords__c = 'test';
        a5.BillingPostalCode = '11111';
        a5.Directory_Display_Override__c = true;
        accounts.add(a5);

        Account a6 = new Account();
        a6.Name = 'Test 6';
        a6.Membership_Level__c = 'Champion';
        a6.Category__c = 'Personal Services';
        a6.Keywords__c = 'farm';
        a6.BillingPostalCode = '22222';
        a6.Directory_Display_Override__c = true;
        accounts.add(a6);

        Account a7 = new Account();
        a7.Name = 'Test 7';
        a7.Membership_Level__c = 'Business';
        a7.Category__c = 'Farms';
        a7.Keywords__c = 'test';
        a7.BillingPostalCode = '11111';
        a7.Directory_Display_Override__c = true;
        accounts.add(a7);

        insert accounts;
    }
  
  @isTest static void testLoadData() {
    Test.startTest();
        MembershipDirectoryModels.FormData fd = MembershipDirectoryController.loadData();
        Test.stopTest();

        System.assertNotEquals(null, fd.categories);
        System.assertEquals(6, fd.spotlightMembers.size());
  }
  
  @isTest static void testSearchMembers() {
    Test.startTest();
        List<Account> categorySearch = MembershipDirectoryController.searchMembers('category', 'Farms');
        List<Account> nameLetterSearch = MembershipDirectoryController.searchMembers('name', 'T');
        List<Account> nameSearch = MembershipDirectoryController.searchMembers('nameSearch', 'Test 3');
        List<Account> keywordSearch = MembershipDirectoryController.searchMembers('keyword', 'test');
        List<Account> zipCodeSearch = MembershipDirectoryController.searchMembers('zipCode', '11111');
        Test.stopTest();

        System.assertEquals(4, categorySearch.size());
        System.assertEquals(7, nameLetterSearch.size());
        System.assertEquals(1, nameSearch.size());
        System.assertEquals(3, keywordSearch.size());
        System.assertEquals(4, zipCodeSearch.size());
  }
  
}


 
Best Answer chosen by John Guerriere
Andrew GAndrew G
bit of code to read, but I notice that in Load data the query is for Will_Display_in_Directory__c  to be not true which you says passes.

in your searchMember your query now searches for Will_Display_in_Directory__c  to be true

Then i review the test data and see Directory_Display_Override__c is the field being set to true.

Is there some relationship between Will_Display_in_Directory__c  and Directory_Display_Override__c  that we are not seeing?

As an alternate, what happens if you add to your test data that Will_Display_in_Directory__c  is true ?


Regards
Andrew

All Answers

Andrew GAndrew G
based on what i'm reading,  I would say check your actual code and review how you are handling the 'zipCode' key word you are passing to that controller.
If it reads OK, trying running the code with 'show coverage' (if your IDE allows) so you can see if the code is being run correctly.  Or use a the debug function to see if the test class is getting into the nitty gritty of that method. 

As a test class, it reads OK. and obviously if the failure is on your 5th assert, then the data being loaded should be ok.

regards
Andrew
mukesh guptamukesh gupta
Hi John,

Issue raised by below  lines 

Please comment this code:- 
 System.assertEquals(4, zipCodeSearch.size());
 System.assertEquals(4, categorySearch.size());

If this solution is usefull for you, Please mark as a Best Answer to help others.


Regards
Mukesh

 
John GuerriereJohn Guerriere

Hi Andrew and Mukesh,

I added the MembershipDirectoryController and MembershipDirectoryModels below.

In the test class I commented out System.assertEquals(4, categorySearch.size()) which is line 90

"Class.MembershipDirectoryControllerTest.testSearchMembers: line 90, column 1"

When "System.assertEquals(4, categorySearch.size());" is commented out the error moves to line 91. All the System.AssertEquals are returning zero.
 

When I change the 4 to 0 line 90 it passes.If I change all to 0 they all pass. So no records are being returned.



        90 System.assertEquals(4, categorySearch.size());
        91 System.assertEquals(7, nameLetterSearch.size());
        92 System.assertEquals(1, nameSearch.size());
        93 System.assertEquals(3, keywordSearch.size());
        94 System.assertEquals(4, zipCodeSearch.size());

Working in the dev console. testLoadData() passes but I can get it to fail if I change the WHERE clause to be = instead of !=

​​​​​​​===
global class MembershipDirectoryController {
  @RemoteAction
  global static MembershipDirectoryModels.FormData loadData() {
    //will need to filter on active members
    List<Account> members = [
      SELECT
        Id,
        Name,
        Description,
        BillingStreet,
        BillingState,
        BillingCity,
        BillingPostalCode,
        BillingLatitude,
        BillingLongitude,
        ShippingStreet,
        ShippingState,
        ShippingCity,
        ShippingPostalCode,
        ShippingLatitude,
        ShippingLongitude,
        Membership_Level__c,
        Logo_URL__c,
        Listing_Page_URL__c
      FROM Account WHERE Will_Display_in_Directory__c=!true


    ];
    List<Account> highRankingMembers = new List<Account>();
    for (Account a : members) {
      if (a.Membership_Level__c == 'Catalyst' || a.Membership_Level__c == 'Champion') {
        highRankingMembers.add(a);
      }
    }
    List<Account> spotlightAccounts = new List<Account>();
    if (highRankingMembers.size() >= 6) {
      for (Integer i = 0; i < 6; i++) {
        spotlightAccounts.add(highRankingMembers.remove((Math.random() * highRankingMembers.size()).intValue()));
      }
    } else {
      for (Integer i = 0; i < highRankingMembers.size(); i++) {
        spotlightAccounts.add(highRankingMembers.remove((Math.random() * highRankingMembers.size()).intValue()));
      }
    }

    MembershipDirectoryModels.FormData fd = new MembershipDirectoryModels.FormData();
    //fd.memberGeocodes = members;
    fd.spotlightMembers = spotlightAccounts;
    fd.categories = getCategories();
    return fd;
  }

  @RemoteAction
  global static List<Account> searchMembers(String searchType, String searchValue) {
    List<Account> matchingMembers = new List<Account>();

    String query = 'SELECT Id, Name, BillingLatitude, BillingLongitude, ShippingLatitude, ShippingLongitude, Category__c, Women_Owned__c, Minority_Owned__c, Veteran_Owned__c, LGBTQ_Owned__c, Membership_Level__c, Membership_Level_Rank__c FROM Account WHERE RecordType.Name = \'Organization\' AND ParentId = NULL AND (Will_Display_in_Directory__c = TRUE OR (Current_Membership__c != NULL AND Current_Membership__r.Migration_Id__c != NULL)) ';
    if (searchType == 'category') {
      query += 'AND (Category__c = \'' + searchValue + '\' OR Category_2__c = \'' + searchValue + '\' OR Category_3__c = \'' + searchValue + '\')';
    } else if (searchType == 'name') {
      query += 'AND Name LIKE \'' + searchValue + '%\'';
    } else if (searchType == 'nameSearch') {
      query += 'AND Name LIKE \'%' + searchValue + '%\'';
    } else if (searchType == 'keyword') {
      query += 'AND Keywords__c LIKE \'%' + searchValue + '%\'';
    } else if (searchType == 'zipCode') {
      query += 'AND (BillingPostalCode = \'' + searchValue + '\' OR ShippingPostalCode = \'' + searchValue + '\')';
    } else if (searchType == 'ownerType') {
      if (searchValue == 'women' || searchValue == 'Women Owned') {
        query += 'AND Women_Owned__c = TRUE';
      } else if (searchValue == 'minority' || searchValue == 'Minority Owned') {
        query += 'AND Minority_Owned__c = TRUE';
      } else if (searchValue == 'veteran' || searchValue == 'Veteran Owned') {
        query += 'AND Veteran_Owned__c = TRUE';
      } else {
        query += 'AND LGBTQ_Owned__c = TRUE';
      }
    }
    query += ' ORDER BY Membership_Level_Rank__c DESC, Name ASC';
    System.debug(query);
    System.debug(searchValue);
    System.debug(searchType);
    matchingMembers = Database.query(query);

    return matchingMembers;
  }

  private static List<MembershipDirectoryModels.Category> getCategories() {
    List<MembershipDirectoryModels.Category> categoryList = new List<MembershipDirectoryModels.Category>();
    List<Schema.PicklistEntry> categoryNames = Account.Category__c.getDescribe().getPicklistValues();
    for (Schema.PicklistEntry entry : categoryNames) {
      MembershipDirectoryModels.Category newCategory = new MembershipDirectoryModels.Category();
      newCategory.name = entry;
      if (entry.getLabel() == 'Advertising, Marketing and Media') {
        newCategory.imageURL = 'https://lowcountrylocalfirst.org/wp-content/uploads/2019/09/LLF_Advertising.png';
      } else if (entry.getLabel() == 'Architecture, Construction and Design') {
        newCategory.imageURL = 'https://lowcountrylocalfirst.org/wp-content/uploads/2019/04/LLF_Architecture.png';
      } else if (entry.getLabel() == 'B-Corporations') {
        newCategory.imageURL = 'https://lowcountrylocalfirst.org/wp-content/uploads/2019/04/LLF_B_Corps.png';
      } else if (entry.getLabel() == 'Business Supplies') {
        newCategory.imageURL = 'https://lowcountrylocalfirst.org/wp-content/uploads/2019/04/LLF_Biz_Supplies.png';
      } else if (entry.getLabel() == 'Education') {
        newCategory.imageURL = 'https://lowcountrylocalfirst.org/wp-content/uploads/2019/04/LLF_Education.png';
      } else if (entry.getLabel() == 'Entertainment and Arts') {
        newCategory.imageURL = 'https://lowcountrylocalfirst.org/wp-content/uploads/2019/04/LLF_Entertainment.png';
      } else if (entry.getLabel() == 'Event Planners and Venues') {
        newCategory.imageURL = 'https://lowcountrylocalfirst.org/wp-content/uploads/2019/04/LLF_Event_Planners.png';
      } else if (entry.getLabel() == 'Farms') {
        newCategory.imageURL = 'https://lowcountrylocalfirst.org/wp-content/uploads/2019/04/LLF_Farms.png';
      } else if (entry.getLabel() == 'Financial Institutions and Services') {
        newCategory.imageURL = 'https://lowcountrylocalfirst.org/wp-content/uploads/2019/04/LLF_Financial.png';
      } else if (entry.getLabel() == 'Food and Beverage') {
        newCategory.imageURL = 'https://lowcountrylocalfirst.org/wp-content/uploads/2019/04/LLF_Food_Bev.png';
      } else if (entry.getLabel() == 'Green Businesses') {
        newCategory.imageURL = 'https://lowcountrylocalfirst.org/wp-content/uploads/2019/04/LLF_Green_Biz.png';
      } else if (entry.getLabel() == 'Health and Wellness') {
        newCategory.imageURL = 'https://lowcountrylocalfirst.org/wp-content/uploads/2019/04/LLF_Health_Wellness.png';
      } else if (entry.getLabel() == 'High Level Members') {
        newCategory.imageURL = 'https://lowcountrylocalfirst.org/wp-content/uploads/2019/04/LLF_Health_Wellness.png';
      } else if (entry.getLabel() == 'Home and Garden') {
        newCategory.imageURL = 'https://lowcountrylocalfirst.org/wp-content/uploads/2019/04/LLF_Home_Garden.png';
      } else if (entry.getLabel() == 'IT and Web Services') {
        newCategory.imageURL = 'https://lowcountrylocalfirst.org/wp-content/uploads/2019/04/LLF_IT_Web.png';
      } else if (entry.getLabel() == 'Lodging and Transportation') {
        newCategory.imageURL = 'https://lowcountrylocalfirst.org/wp-content/uploads/2019/04/LLF_Lodging.png';
      } else if (entry.getLabel() == 'Manufacturing and Product Development') {
        newCategory.imageURL = 'https://lowcountrylocalfirst.org/wp-content/uploads/2019/04/LLF_Manufacturing.png';
      } else if (entry.getLabel() == 'Nonprofit Organizations') {
        newCategory.imageURL = 'https://lowcountrylocalfirst.org/wp-content/uploads/2019/04/LLF_Nonprofit.png';
      } else if (entry.getLabel() == 'Personal Services') {
        newCategory.imageURL = 'https://lowcountrylocalfirst.org/wp-content/uploads/2019/04/LLF_Personal_Services.png';
      } else if (entry.getLabel() == 'Pet Services and Supplies') {
        newCategory.imageURL = 'https://lowcountrylocalfirst.org/wp-content/uploads/2019/04/LLF_Pets.png';
      } else if (entry.getLabel() == 'Professional Services') {
        newCategory.imageURL = 'https://lowcountrylocalfirst.org/wp-content/uploads/2019/04/LLF_Professional_Services.png';
      } else if (entry.getLabel() == 'Real Estate') {
        newCategory.imageURL = 'https://lowcountrylocalfirst.org/wp-content/uploads/2019/04/LLF_Real_Estate.png';
      } else if (entry.getLabel() == 'Retail, Gifts, Clothing and Accessories') {
        newCategory.imageURL = 'https://lowcountrylocalfirst.org/wp-content/uploads/2019/04/LLF_Retail.png';
      }

      categoryList.add(newCategory);
    }
    return categoryList;
  }
}

===


global class MembershipDirectoryModels {
    global class FormData {
        //public List<Account> memberGeocodes;
        public List<Account> spotlightMembers {get; set;}
        public List<Category> categories {get; set;}
    }

    global class Category {
        public Schema.PicklistEntry name {get; set;}
        public String imageURL {get; set;}
    }
}

Andrew GAndrew G
bit of code to read, but I notice that in Load data the query is for Will_Display_in_Directory__c  to be not true which you says passes.

in your searchMember your query now searches for Will_Display_in_Directory__c  to be true

Then i review the test data and see Directory_Display_Override__c is the field being set to true.

Is there some relationship between Will_Display_in_Directory__c  and Directory_Display_Override__c  that we are not seeing?

As an alternate, what happens if you add to your test data that Will_Display_in_Directory__c  is true ?


Regards
Andrew
This was selected as the best answer
John GuerriereJohn Guerriere

Hi Andrew,
You got it. Issue is the Display Override was used by the Will_Display_in_Directory formula field when the class and test was first written. The formula did not work correctly so it was changed. Long story short, there is a new field Display_In_Directory_Override__c that need to be added to the CreateData method in the Test class. The new field sets the Display Formula to be true. 

I am volunteering for a local nonprofit and picking up an org that was set-up over a year ago. There have been changes made along the way by different people so it's kind of evolved into a ball of yarn.

So all good now though. Test pass. 

Thank you for the help!!
 

John

        Account a7 = new Account();
        a7.Name = 'Test 7';
        a7.Membership_Level__c = 'Business';
        a7.Category__c = 'Farms';
        a7.Keywords__c = 'test';
        a7.BillingPostalCode = '11111';
        a7.Directory_Display_Override__c = true; //This does not do anything anymore. 
addrd this---- a7.Display_In_Directory_Override__c ='Display'; //This sets the Will_Display_in_Directory to True
        accounts.add(a7);