title: Apex test class for trigger - beginners guide
body: <p>I am relatively inexperienced with coding and have never been been forced to write test classes before. I do not understand it conceptually. I have written the following trigger: </p>
  
  <pre><code>trigger CreateNewInvoiceAndUpdateCM on CampaignMember(after insert, after update){
  for (CampaignMember cm: trigger.new){
  
      if(cm.Status == 'Needs Renewal and Invoice'){
          Asset memb = [SELECT Id, Product2Id FROM Asset WHERE Id = :cm.Membership__c];
          Product2 prod = [SELECT Id, ProductCode FROM Product2 WHERE Id = :memb.Product2Id];
          Pricebook2 pb =[SELECT Id, Name FROM Pricebook2 WHERE Name = 'Standard Price Book'];
          PricebookEntry pbe = [SELECT Id, UnitPrice, Pricebook2Id, Product2Id FROM PricebookEntry WHERE Pricebook2Id = :pb.Id AND Product2Id = :prod.Id];
  
          Invoice__c inv = new Invoice__c();
          inv.Membership__c = cm.Membership__c;
          inv.Contact__c = cm.ContactId;
          inv.Price__c = pbe.UnitPrice;
          insert inv;
  
  
  
          CampaignMember newCm = [select Id, Invoice__c FROM CampaignMember WHERE Id = :cm.Id];
          newCm.Invoice__c = inv.Id;
          newCM.Status = 'Needs Renewal';
          update newCm;
  
          }}
          }
  </code></pre>
  
  <p>But then I have no idea how to properly write a test class. I have gone through all the documentation on the Salesforce site and countless posts on here but I have failed to get my code coverage up from 13% (2/15)[What does this mean btw?].</p>
  
  <p>The test class I have written is basically just an attempt to get everything in there in the hope that the code coverage will go up. I have no idea what I'm actually supposed to be doing with the test class:</p>
  
  <pre><code>@isTest 
  
  private class testCreateNewInvoiceAndUpdateCM{
  
  static testMethod void testCreateNewInvoiceAndUpdateCM(){
  
  PricebookEntry pbe = new PriceBookEntry();
  pbe.UnitPrice = 2;
  
  Campaign camp = new Campaign();
  camp.Name = 'Test Campaign';
  insert camp;
  
  Account acc = new Account();
  acc.Name ='Test Account';
  insert acc;
  
  Contact cont= new Contact();
  cont.LastName = 'LastName';
  cont.AccountId = acc.Id;
  insert cont;
  
  
  Asset ass = new Asset();
  ass.Name = 'Test Asset';
  ass.Membership_Start_Date__c = System.today();
  ass.ContactId = cont.Id;
  ass.AccountId = ass.Id;
  insert ass;
  
  Invoice__c inv = new Invoice__c();
  inv.Membership__c = ass.Id;
  inv.Contact__c = cont.Id;
  inv.Price__c = pbe.UnitPrice;
  insert inv;
  
  CampaignMember cm = new CampaignMember(); 
  cm.CampaignId = camp.Id;
  cm.ContactId = cont.Id;
  cm.Membership__c = ass.Id;
  cm.Status = 'Needs Renewal and Invoice';
  cm.Invoice__c = Inv.Id;
  
  insert cm;
  
  
  CampaignMember cm2 = [select Id, Status, Invoice__c  from CampaignMember where Id = :cm.Id];
  System.assertEquals('Needs Renewal and Invoice',cm.Status);
  
  
  
  
  
  
  
     }
  }
  </code></pre>
  
  <p>Can anyone help me please it's driving me insane?</p>