I am writing a test for some code I wrote for a trigger. I create all of the data in the test, and have SeeAllData = false. The test class invokes the trigger, but a query in the trigger helper code returns 0 results. I don't understand why, with SeeAllData = false I was lead to believe my code could find the test data in the query during the run.
Here is my test code, so far -
static testMethod void createSubscriptionsFromSubDetails() {
Test.startTest();
Product2 p = new Product2();
p.name = 'Test Product';
p.Family = 'Purchase Option';
p.ProductCode = 'HW-0101';
insert p;
Account acct = new Account(Name='Test Account');
acct.Last_Subscription_Detail_Update__c = DateTime.now();
insert acct;
List<SubscriptionDetails__c> subDetails = new List<SubscriptionDetails__c>();
for(integer i = 1; i <= 5; i++)
{
SubscriptionDetails__c s = new SubscriptionDetails__c();
s.Name = 'Test Sub Detail' + i;
s.AccountId__c = acct.ID;
s.StartDate__c = Date.newInstance(2020, 1, 9);
s.EndDate__c = Date.newInstance(2023, 1, 9);
s.Price__c = 500;
s.SalesforceProductCode__c = p.productcode;
subDetails.add(s);
}
insert subDetails;
//Update the account so the trigger fires
Account insertedAccount = [Select Name, OwnerId from Account where Id =: acct.Id];
insertedAccount.Last_Subscription_Detail_Update__c = DateTime.now();
update insertedAccount;
system.debug('test setup : ' + insertedAccount.ID);
Test.stopTest();
}
This eventually ends up in some code in a separate class doing this:
public void createContracts(){
subscriptionDetails = [SELECT name, ApplicationFeatureName__c, id, SalesforceProductCode__c,
StartDate__c, EndDate__c, Price__c
FROM SubscriptionDetails__c
WHERE ApplicationFeatureName__c != ''
AND AccountId__c =: accountID];
if(subscriptionDetails.size() > 0)
{
......
}
But I see that subscriptionDetails is returning 0 results, so it doesn't have access to this data. (The account ID is correct, I debugged that)
Can anyone explain why? How do I set this up so my other code getting called during the test can see the test data?
Thanks