|
| 1 | +/** |
| 2 | + * TestDataFactoryTest tests TestDataFactory |
| 3 | + */ |
| 4 | +@isTest |
| 5 | +private class TestDataFactoryTest { |
| 6 | + |
| 7 | + @isTest |
| 8 | + static void testGetAccount() { |
| 9 | + // Prepare the test data |
| 10 | + Account acc = TestDataFactory.getAccount('Test Account', false); |
| 11 | + Database.insert(acc); |
| 12 | + |
| 13 | + // Perform the test |
| 14 | + Test.startTest(); |
| 15 | + List<Account> accts = [SELECT Id FROM Account]; |
| 16 | + Test.stopTest(); |
| 17 | + |
| 18 | + // Assertions |
| 19 | + Assert.isTrue(accts.size() > 0, 'Expected to find at least one account'); |
| 20 | + } |
| 21 | + |
| 22 | + @isTest |
| 23 | + static void testGenerateAccountWithContacts() { |
| 24 | + // Prepare the test data |
| 25 | + List<Account> accts; |
| 26 | + List<Contact> contacts; |
| 27 | + TestDataFactory.generateAccountWithContacts(2, 1); |
| 28 | + |
| 29 | + // Perform the test |
| 30 | + Test.startTest(); |
| 31 | + accts = [SELECT Id FROM Account]; |
| 32 | + contacts = [SELECT Id, Email FROM Contact]; |
| 33 | + Test.stopTest(); |
| 34 | + |
| 35 | + // Assertions |
| 36 | + Assert.isTrue(accts.size() > 0, 'Was expecting to find at least one account'); |
| 37 | + Assert.areEqual(2, contacts.size(), 'Was expecting to find 2 contacts'); |
| 38 | + Assert. areEqual( '[email protected]', contacts[ 0]. Email, 'Expected [email protected] Email'); |
| 39 | + Assert. areEqual( '[email protected]', contacts[ 1]. Email, 'Expected [email protected] Email'); |
| 40 | + } |
| 41 | + |
| 42 | + @isTest |
| 43 | + static void testCreateLeadsByTitle() { |
| 44 | + // Prepare the test data |
| 45 | + List<String> titles = new List<String>{'vp', 'v.p.', 'vice president'}; |
| 46 | + |
| 47 | + // Perform the test |
| 48 | + Test.startTest(); |
| 49 | + List<Lead> leads = TestDataFactory.createLeadsByTitle(titles, false); |
| 50 | + Test.stopTest(); |
| 51 | + |
| 52 | + // Assertions |
| 53 | + Assert.isTrue(leads.size() > 0, 'Was expecting to find at least one Lead'); |
| 54 | + Assert.areEqual(3, leads.size(), 'Was expecting to find 3 Leads'); |
| 55 | + Assert.areEqual('vp', leads[0].Title, 'Expected Title = \'vp\''); |
| 56 | + Assert.areEqual('v.p.', leads[1].Title, 'Expected Title = \'v.p.\''); |
| 57 | + Assert.areEqual('vice president', leads[2].Title, 'Expected Title = \'vice president\''); |
| 58 | + } |
| 59 | + |
| 60 | + @isTest |
| 61 | + static void testCreateLeadsByParams() { |
| 62 | + // Prepare the test data |
| 63 | + List<Map<String, Object>> params = new List<Map<String, Object>>(); |
| 64 | + params. add( new Map< String, Object>{ 'LeadSource' => 'Web', 'Email' => '[email protected]', |
| 65 | + 'Phone' => '(908)346-1234', 'Industry' => 'Technology'}); |
| 66 | + |
| 67 | + // Perform the test |
| 68 | + Test.startTest(); |
| 69 | + List<Lead> newLeads = TestDataFactory.createLeadsByParams(params, true); |
| 70 | + Test.stopTest(); |
| 71 | + |
| 72 | + // Retrieve data |
| 73 | + List<Lead> leads = [ |
| 74 | + SELECT |
| 75 | + LeadSource, |
| 76 | + Email, |
| 77 | + Phone, |
| 78 | + Industry |
| 79 | + FROM Lead |
| 80 | + ]; |
| 81 | + |
| 82 | + // Assertions |
| 83 | + Assert.isTrue(leads.size() > 0, 'Was expecting to find at least one Lead'); |
| 84 | + Assert.areEqual(1, leads.size(), 'Was expecting to find 1 Lead'); |
| 85 | + Assert.areEqual('Web', leads[0].LeadSource, 'Expected Lead Source = \'Web\''); |
| 86 | + Assert. areEqual( '[email protected]', leads[ 0]. Email, 'Expected Email = \'[email protected]\''); |
| 87 | + Assert.areEqual('(908)346-1234', leads[0].Phone, 'Expected Phone = \'(908)346-1234\''); |
| 88 | + Assert.areEqual('Technology', leads[0].Industry, 'Expected Industry = \'Technology\''); |
| 89 | + } |
| 90 | + |
| 91 | + @isTest |
| 92 | + static void testCreateLeadsWithParams() { |
| 93 | + // Prepare the test data |
| 94 | + Map<String, Object> params = new Map<String, Object>{'LeadSource' => 'Web', 'Email' => 'test'}; |
| 95 | + |
| 96 | + // Perform the test |
| 97 | + Test.startTest(); |
| 98 | + List<Lead> leads = TestDataFactory.createLeadsWithParams(1, params, false); |
| 99 | + Test.stopTest(); |
| 100 | + |
| 101 | + // Assertions |
| 102 | + Assert.isTrue(leads.size() > 0, 'Was expecting to find at least one Lead'); |
| 103 | + Assert.areEqual(1, leads.size(), 'Was expecting to find 1 Lead'); |
| 104 | + Assert.areEqual('Web', leads[0].LeadSource, 'Expected Lead Source = \'Web\''); |
| 105 | + Assert. areEqual( '[email protected]', leads[ 0]. Email, 'Expected Email = \'[email protected]\''); |
| 106 | + } |
| 107 | +} |
0 commit comments