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
Ivan WinzerIvan Winzer 

My test class

So below is the test class created to test my credit card trigger. I keep getting a assert error but looking at it im wondering if im doing way more then needed in the test. Below is the test class 

 @isTest
 public class Test_CreditCardTrigger{
    @isTest
    public static void testCreateCreditCard(){
        Account a = new Account(Name = 'Test MainContactTrigger');
        insert a;
        Contact c = new Contact(AccountId = a.id, firstName = 'Test MainContactTrigger', lastName = 'Tester');
        insert c;
        authnet_credit_card__c cc = new authnet_credit_card__c( Contact__c = c.id);
        insert cc;
        c = [select AccountId,Account.Name from Contact where id = :c.id limit 1];
           System.assertEquals('Test MainContactTrigger', c.Account.Name);
           System.assertEquals(a.id, c.AccountId);
    }

 }
Ivan WinzerIvan Winzer
This is the errro im getting

 System.AssertException: Assertion Failed: Expected: Test MainContactTrigger, Actual: Test MainContactTrigger Tester
AshlekhAshlekh
Hi,

The below code is working fine. I commented authnet_credit_card__c insertion because I don't have this object in  my org.
@isTest
 public class Test_CreditCardTrigger{
    @isTest
    public static void testCreateCreditCard(){
        Account a = new Account(Name = 'Test MainContactTrigger');
        insert a;
        Contact c = new Contact(AccountId = a.id, firstName = 'Test MainContactTrigger', lastName = 'Tester');
        insert c;
        //authnet_credit_card__c cc = new authnet_credit_card__c( Contact__c = c.id);
        //insert cc;
        c = [select AccountId,Account.Name from Contact where id = :c.id limit 1];
        system.debug('-------------'+c);
           System.assertEquals('Test MainContactTrigger', c.Account.Name);
           System.assertEquals(a.id, c.AccountId);
    }

 }
Where are you getting error
Ivan WinzerIvan Winzer
The error is saying that the Assert failed, i added the error message above.

 
Ivan WinzerIvan Winzer
Its at line 12 column 1 (but in your above snap shot it would be 13 column 1)
Chinmay BhusariChinmay Bhusari
Hi,
in contact the name of lookup field is Account not AccountId. Maybe that's where the problem is.

Regards
Ivan WinzerIvan Winzer
yea when i remove ID from it i get another error. The accountID is grabbing and holding the info for the contact record and then checked in the assert at the bottom.
Ivan WinzerIvan Winzer
So Chinmay and  D-Horse so i figured out the first assert issue the account name needed to be a.name not c.account.name. Now that is fixed i get a new error for the second assert

 System.AssertException: Assertion Failed: Expected: 001e000000LMW5GAAX, Actual: 001e000000LMW5HAAX

which i know means the account ID that was created for the account does no match the one for the contact but what i dont see is why if im creating the contact on that account.
Chinmay BhusariChinmay Bhusari
Hi Ivan,
Intially when you are creating the contact use

Account = a.id

and in the query also use Account.

and if still this does not work re-Query the value of the id form the database.
I am quite sure that it will work.
And if it still does'nt send me your updated code.

Regards
Ivan WinzerIvan Winzer
Chinmay,

Everytime i make the change as you specified above with Account = a.id i get the error of not being able to use a sobject in single querie.

 
Ivan WinzerIvan Winzer

Error: Compile Error: Invalid initial expression type for field Account, expecting: SOBJECT:Account (or single row query result of that type) at line 8 column 43


Ivan WinzerIvan Winzer
So i figured out my first issue but now i cant get the code coverage higher then 72%. it keeps saying that i still have 5 lines not being hit from line 18 down. Can anyone see why these 5 lines are not being hit and how i can get them to be hit and get at least 75% coverage.

trigger CredCardTrigger on authnet_credit_card__c (After Insert, After Update) { 
    List<authnet_credit_card__c > slist = trigger.New; 
    List<authnet_credit_card__c> oldPrimaryList = new List<authnet_credit_card__c>(); 
    List<Contact> contactsToUpdate = new List<Contact>(); 

    for ( authnet_credit_card__c a : slist ) {  
        if ( a.isPrimaryWineClubCard__c == true) { 
            system.debug('Copying in primary Auth id '+a.Payment_Id__c); 
            Contact c = New Contact(Id = a.Contact__c); 

            if (c.authnet_profile_id__c != null) { 
                if (c.authnet_profile_id__c != a.Profile_Id__c) { 
                    authnet_credit_card__c oldPrimary = new authnet_credit_card__c(Profile_Id__c = c.authnet_profile_id__c); 
                    system.debug('Unchecking old authnet primary id '+a.Profile_Id__c); 
                    oldPrimary.IsPrimaryWineClubCard__c = false; 
                    oldPrimaryList.add(oldPrimary); 
                } 
            } 

            c.authnet_profile_id__c = a.Profile_Id__c; 
            c.authnet_default_payment_id__c = a.Payment_Id__c; 
            contactsToUpdate.add(c); 

        } 
    } 

   if (!oldPrimaryList.isEmpty()) { 
        update oldPrimaryList;