Skip to content

Instantly share code, notes, and snippets.

@kamalpreet-rad
Last active December 7, 2023 00:38
Show Gist options
  • Save kamalpreet-rad/40846a46d8e36fdfa5fd9a1166af74f2 to your computer and use it in GitHub Desktop.
Save kamalpreet-rad/40846a46d8e36fdfa5fd9a1166af74f2 to your computer and use it in GitHub Desktop.
Week 6
@isTest
public class RecipeHandler_Testcls1 {
@istest
static void negativeCheckDraftRecipe(){
//If a recipe is inserted WITH all required fields, it should not automatically be marked as draft
Recipe__c r = new Recipe__c (
Name='Pan Cakes',
Active_Time__c = 2,
Active_Time_Units__c = 'Hours',
Description__c = 'Recipe is created with all the required fields',
Servings__c = 4
);
try{
Test.startTest();
insert r;
}catch(Exception e){
Boolean exceptionContainsMessage = e.getMessage().contains('This recipe is missing one or more of the following required fields: Name, Active Time, Active Time Units, Servings, Description');
Assert.isTrue(exceptionContainsMessage, 'There should have been an error about missing fields');
}
Test.stopTest();
// Query for the inserted Recipe SObject
Recipe__c insertedRecipe = [SELECT ID, Draft__c FROM Recipe__c WHERE Name='Pan Cakes' LIMIT 1];
//Check the draft status for the recipe
System.debug(insertedRecipe.Draft__c);
System.assertEquals(false, insertedRecipe.Draft__c, 'A Recipe with all required fields should NOT be marked as Draft');
}
@isTest
static void testExceptionDraftRecipe(){
// Create Recipe with missing required fields and throws an exception
Boolean exceptionThrown = false;
Recipe__c r = new Recipe__c (
Name='New Pan Cakes',
Active_Time__c = 1,
Active_Time_Units__c = 'Minutes',
Description__c = '1 lb Pan Cake Mixture.'
);
try{
Test.startTest();
insert rec;
Assert.fail('An Exception should have been thrown');
//System.debug('Exception thrown');
}
catch(Exception e){
exceptionThrown = true;
Boolean expectedExceptionThrown = (e.getMessage().contains('Required field(s) are missing')) ? true : false;
System.debug('Exception thrown');
System.AssertEquals(true, expectedExceptionThrown, e.getMessage());
System.debug(e.getMessage()); // printing the addError message
}
Test.stopTest();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment