You need to sign in to do that
Don't have an account?
Trigger seemingly not acting on test case update
Hi all,
This may end up being a dumb question, but I'm very grateful for any help! I have a trigger that I'm trying to test, and though it works when I test it manually via a UI case update, I can't get the test to work at all.
The trigger is designed to catch cases before update and, if the associated asset has changed, update a field on the case to match a corresponding field on its asset. Here's a snippet of the test:
[test cases and assets created and inserted above, including optedInAsset and caseForOptedOutAsset ...]
Case updatedVersion1 = [Select Id, Opt_In__c, AssetId from Case where Id=:caseForOptedOutAsset.Id limit 1];
updatedVersion1.AssetId = ID.valueOf(optedInAsset.Id);
update updatedVersion1;
updatedVersion1 = [Select Id, Opt_In__c, AssetId from Case where Id=:updatedVersion1.Id limit 1];
System.assertEquals(true, updatedVersion1.Opt_In__c);
[...]
For some reason, this assert always fails, though, again, it works smoothly in the UI. I don't seem to even be able to force it--it's like the "before update" trigger is being ignored, and the AssetId field is just being updated without firing any trigger.
Is there anything that could cause a trigger to not fire or be somehow truncated in the context of an Apex test?
Thanks again!
Could you post your code? Also, are you inserting a new Case record for your test or querying one from the database? Best practices say you should always insert the records your are going to be testing. Is it possible that you are querying a record that does not meet your opt-in criteria?
Hi Kevin,
Thank you! The trigger I've added this functionality to is a bit of a beast, but I'll scrub it down to the essentials. I am inserting the records that I'm testing, so there's a chance that's the issue, but I'd put in additional asserts to verify that 1) the case's asset was changing to the right one after the update and 2) that asset did meet the criteria that should cause the case field to update.
Trigger:
Test: