0

The advice in Testing Custom Metadata Types is to add metadata records for unit tests if necessary. We need our managed package customers to be able to add additional records, so having the test-only records visible to them is going to be a bit confusing and inelegant.

Is there a way to hide or (automatically) remove these test-only records so they are not there in orgs into which the managed package is installed?

  • Not really; I'd say best practice would be to set up your code in a way that you can mock metadata records. Also, I think in Summer 18 we can insert metadata records in Apex Code? – sfdcfox May 11 '18 at 16:52
  • @sfdcfox Thanks. Unfortunately the Apex insertion is async and doesn't work in tests. Would like to avoid mocking because a fairly complex query is used and I want to test that too. – Keith C May 11 '18 at 16:56
  • Yeah, I don't think there's a way to have your cake and eat it, too, though I'd love to be proven wrong. – sfdcfox May 11 '18 at 16:58
1

You can simply make the records protected. Then subscribers won't be able to see them.

Or do you mean they need to be hidden from your own application outside of test contexts? I covered that case in a blog pose here (see the section "Testing Global Custom Metadata").

  • 1
    I'll just add to this - I added an "isTest" Boolean field (but omitted it from the MDT page layout) because SOQL doesn't yet support querying whether a record is Protected. I only ever query the MDT using a dedicated class. The class is configured that in unit tests it queries only isTest = true records, and outside of unit tests it queries only isTest = false records. – Charles T May 12 '18 at 18:54
  • Its the subscribers I want to hide them through so cool, thanks. – Keith C May 14 '18 at 10:17
  • @CharlesT Also my colleague suggested making the IsTest field protected to hide that from the subscriber org. – Keith C May 14 '18 at 10:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.