0

Hi and thanks in advance. I created my first trigger and it does what it is supposed to do. The problem comes in when creating a test.

Please look at this and offer advice.

trigger Reg_Request_to_Reg on Registration_Request__c (after insert) {
    List<Registration__c> newRegistration = new List <Registration__c>();
        for (Registration_Request__c reg : trigger.new) {

            Registration__c newReg = new Registration__c();
            newReg.Account_Name__c = reg.Account__c;
            newReg.Opportunity_Name__c = reg.Opportunity_Name__c;
            newReg.Product__c = reg.Product__c;
            newReg.Manufacturer__c = reg.Manufacturer__c;
            newRegistration.add(newReg);

        }
    insert newRegistration;
}

    @isTest
    public class UpdateRegistrationfromRegRequest 
    {
         static testMethod void testInsertintoReg  ()  {
             //Set up the Account record.
            Registration_Request__c a = new Registration_Request__c (Account__c='krista test');
            insert a;

            //set up the Registration Request record
            a = [Select Name, Account__c FROM Registration_Request__c WHERE Account__c = :a.Account__c];
            System.assertEquals('this is a good test', a.Account__c);

            //Set up the Registration record     
            String RegistrationName = 'Eric';
            Registration__c r = new Registration__c(Account_Name__c=a.Account__c, Opportunity_Name__c=a.Opportunity_Name__c);

            //Cause the trigger to execute
            insert a;
            System.debug('Price before inserting new book: ' + r.Account_Name__c);


            //Verify results are as expected
            a=[Select Account__c FROM Registration_Request__c WHERE Account__c = :a.Account__c];
            System.assertEquals('this is the second test', a.Account__c);

    }
}
1
  • 1
    Welcome to StackExchange. In order to better assist you can you elaborate on what problem you're facing? IS there an error?
    – greenstork
    Commented Oct 3, 2013 at 17:08

1 Answer 1

1

You are not creating any of your test data. I assume you are getting some sort of Invalid Cross Reference error.

Adjust your test code to something like this. Haven't tested it, but should work

@isTest
    public class UpdateRegistrationfromRegRequest 
    {
         static testMethod void testInsertintoReg  ()  {

            //Create your data       

            Account a = new Account(Name = 'Test Account');
            insert a;

            Opportunity o = new Opportunity(Name = 'Test Opp', AccountId = a.id, StageName = 'Your Stage Name', CloseDate = system.today().adddays(7));
            insert o;

            Product__c p = new Product__c(Name= 'Test Product');
            insert p;

            Manufacturer__c  m = new Manufacturer__c(Name = 'Test Manufacturer');
            insert m;

            //Now you need to create your Registration Request to test the trigger
            Registration_Request__c request = new Registration_Request__c (Account__c = a.Id, Opportunity_Name__c = o.Name, Product__c = p.Id, Manufacturer__c = m.Id);
            insert req;

            //Now we need to test to make sure the registration was created
            Registration__c registration = [Select Id, Name, Account__c, Opportunity_Name__c, Product__c, Manufacturer__c From Registration__c Limit 1];

            system.assertEquals(registration.Account__c, request.Account__c);
            system.assertEquals(registration.Opportunity_Name__c, request.Opportunity_Name__c);
            system.assertEquals(registration.Product__c, request.Product__c);
            system.assertEquals(registration.Manufacturer__c, request.Manufacturer__c);

    }
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .