How to test a nested map attribute?


I have a nested map attribute that I want to validate in my tests:

		"domains": schema.MapNestedAttribute{
			MarkdownDescription: "...",
			Required:            true,
			NestedObject: schema.NestedAttributeObject{
				Attributes: map[string]schema.Attribute{
					"comment": schema.StringAttribute{
						MarkdownDescription: "...",
						Optional:            true,

If this was a set, then in my tests I’d validate the set by checking its length like…

resource.TestCheckResourceAttr("example.test", "domains.#", "2"),

But in the case of a ‘map’ type, I’m not sure how you would assert it contained for example two keys?

Any guidance appreciated. Thanks!

Hi @Integralist :wave:

The equivalent for testing the length of a map is as follows:

resource.TestCheckResourceAttr("example.test", "domains.%", "2"),

See TestCheckResourceAttr.

Awesome. Thank you! :slightly_smiling_face: