Dynamically creating mocks from data? #2415
-
I'm mocking filtered calls to $User = Get-MgUser -UserId $RoleAssignment.PrincipalId And like this in the test file $MockLookup = @{}
foreach ($User in $UserData) {
$MockLookup[$User.Id] = $User
}
Mock Get-MgUser { $Mocklookup['787af109-31dc-4adf-9c7d-fd310c04360a'] } -ParameterFilter { $UserId -eq '787af109-31dc-4adf-9c7d-fd310c04360a' }
Mock Get-MgUser { $Mocklookup['1530541d-810b-4c8b-b33c-27136e72255b'] } -ParameterFilter { $UserId -eq '1530541d-810b-4c8b-b33c-27136e72255b' }
Mock Get-MgUser { $Mocklookup['f4ae2b54-2052-4fdd-a9a5-c2955d4cf65c'] } -ParameterFilter { $UserId -eq 'f4ae2b54-2052-4fdd-a9a5-c2955d4cf65c' }
Mock Get-MgUser { $Mocklookup['01fcdc0e-d4db-404e-83c8-3d4f36bb7c28'] } -ParameterFilter { $UserId -eq '01fcdc0e-d4db-404e-83c8-3d4f36bb7c28' }
Mock Get-MgUser { $Mocklookup['3c962d06-8d15-4e32-a575-ee99cb9b1b44'] } -ParameterFilter { $UserId -eq '3c962d06-8d15-4e32-a575-ee99cb9b1b44' }
Mock Get-MgUser { $Mocklookup['e815063e-9339-4d96-b28b-c475afa6a7c5'] } -ParameterFilter { $UserId -eq 'e815063e-9339-4d96-b28b-c475afa6a7c5' }
Mock Get-MgUser { $Mocklookup['551940b1-70b6-4f20-847b-1853466664ad'] } -ParameterFilter { $UserId -eq '551940b1-70b6-4f20-847b-1853466664ad' }
Mock Get-MgUser { $Mocklookup['ce5f1cd6-70bd-4482-a38f-37c47cdad0d6'] } -ParameterFilter { $UserId -eq 'ce5f1cd6-70bd-4482-a38f-37c47cdad0d6' }
Mock Get-MgUser { $Mocklookup['bec6779f-ab5d-42de-920f-62c65ea6eb39'] } -ParameterFilter { $UserId -eq 'bec6779f-ab5d-42de-920f-62c65ea6eb39' }
Mock Get-MgUser { $Mocklookup['bd3cc1b4-3c46-4b1f-ad54-a3daeb49b1d6'] } -ParameterFilter { $UserId -eq 'bd3cc1b4-3c46-4b1f-ad54-a3daeb49b1d6' }
Mock Get-MgUser { $Mocklookup['a4781600-3d02-4de7-a2b2-5644b8ed69b2'] } -ParameterFilter { $UserId -eq 'a4781600-3d02-4de7-a2b2-5644b8ed69b2' }
Mock Get-MgUser { $Mocklookup['46e1d71f-0b21-46eb-a562-90709e1695a2'] } -ParameterFilter { $UserId -eq '46e1d71f-0b21-46eb-a562-90709e1695a2' }
Mock Get-MgUser { $Mocklookup['5d07cf6c-b3fd-4208-85ab-3a7c3f8c7747'] } -ParameterFilter { $UserId -eq '5d07cf6c-b3fd-4208-85ab-3a7c3f8c7747' }
This works, but is obviously horrible. I imagine there is a way to generate mocks dynamically, with a naive implementation (that doesn't work ) like: $mockLookup.GetEnumerator() | ForEach-Object {
Mock Get-MgUser { $MockLookup[$($_.Key)] } -ParameterFilter { $UserId -eq $($_.Value.Id) }
} but I suspect scoping nuances beyond my understanding are at play. It feels like a riff on this but I've not managed to transpose it... Any help, much appreciated. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
Is this a simplified example? Looks to me that you could simply add a single mock with no filter and return the data from the $MockLookup table based on $UserId Mock Get-MgUser { $Mocklookup[$UserId] } |
Beta Was this translation helpful? Give feedback.
-
Apologies for the brevity, but I think you got it anyway - that seems to work! Ok, so am I right in thinking that the values passed to parameters on the "real" command (i.e. I'm assuming that's how the mock can lookup the key in the lookup table in the testfile?
Actually, "available" is the wrong word I guess; is the parameter value being resolved in the test scope when the mock gets registered?
Or am I talking rubbish? 🤣 Must go and read all the docs again. |
Beta Was this translation helpful? Give feedback.
Is this a simplified example? Looks to me that you could simply add a single mock with no filter and return the data from the $MockLookup table based on $UserId