I'm currently struggling to get unit tests to work...
# dotnet
b
I'm currently struggling to get unit tests to work in C#. Using the default mocks and getting a lot of null reference exceptions. On calls like
var credentials = ListRegistryCredentials.Invoke(new ListRegistryCredentialsInvokeArgs
{
ResourceGroupName = _azureConfig.ResourceGroupName,
RegistryName = $"{_azureConfig.Stack.ToLower()}services{_azureConfig.Env}cr"
});
This and all other calls work fine otherwise.
The mocks:
var mocks = new Mock<IMocks>();
mocks.Setup(m => m.NewResourceAsync(It.IsAny<MockResourceArgs>()))
.ReturnsAsync((MockResourceArgs args) => (args.Id ?? "", args.Inputs));
mocks.Setup(m => m.CallAsync(It.IsAny<MockCallArgs>()))
.ReturnsAsync((MockCallArgs args) => args.Args);
return Deployment.TestWithServiceProviderAsync<T>(mocks.Object, serviceProvider,new TestOptions { IsPreview = false });
b
Do you know what is null? Can you tell from the error? Is it your azure config or one of its properties that is null?
b
This is the exception it throws:Object reference not set to an instance of an object. at T System.Linq.ImmutableArrayExtensions.First<T>(ImmutableArray<T> immutableArray) at Credentials MultiCloudPulumi.Core.Azure.Services.AzureContainerService.GetContainerRegistryCredentials()+(ListRegistryCredentialsResult c) => { } in C/dev/MultiCloud/Infrastructure/MultiCloudPulumi.Core.Azure/Services/AzureContainerService.csline 42 at async Task<OutputData<U>> Pulumi.Output<T>.ApplyHelperAsync<U>(Task<OutputData<T>> dataTask, Func<T, Output<U>> func) at async Task<OutputData<ValueTuple<T1, T2, T3, T4, T5, T6, T7, ValueTuple<T8>>>> Pulumi.Output<T>.TupleHelperAsync<T1, T2, T3, T4, T5, T6, T7, T8>(Input<T1> item1, Input<T2> item2, Input<T3> item3, Input<T4> item4, Input<T5> item5, Input<T6> item6, Input<T7> item7, Input<T8> item8)+GetData(?) at async Task<OutputData<ValueTuple<T1, T2, T3, T4, T5, T6, T7, ValueTuple<T8>>>> Pulumi.Output<T>.TupleHelperAsync<T1, T2, T3, T4, T5, T6, T7, T8>(Input<T1> item1, Input<T2> item2, Input<T3> item3, Input<T4> item4, Input<T5> item5, Input<T6> item6, Input<T7> item7, Input<T8> item8) at async Task<OutputData<U>> Pulumi.Output<T>.ApplyHelperAsync<U>(Task<OutputData<T>> dataTask, Func<T, Output<U>> func) x 4 at async Task<OutputData<ValueTuple<T1, T2, T3, T4, T5, T6, T7, ValueTuple<T8>>>> Pulumi.Output<T>.TupleHelperAsync<T1, T2, T3, T4, T5, T6, T7, T8>(Input<T1> item1, Input<T2> item2, Input<T3> item3, Input<T4> item4, Input<T5> item5, Input<T6> item6, Input<T7> item7, Input<T8> item8)+GetData(?) at async Task<OutputData<ValueTuple<T1, T2, T3, T4, T5, T6, T7, ValueTuple<T8>>>> Pulumi.Output<T>.TupleHelperAsync<T1, T2, T3, T4, T5, T6, T7, T8>(Input<T1> item1, Input<T2> item2, Input<T3> item3, Input<T4> item4, Input<T5> item5, Input<T6> item6, Input<T7> item7, Input<T8> item8) at async Task<OutputData<U>> Pulumi.Output<T>.ApplyHelperAsync<U>(Task<OutputData<T>> dataTask, Func<T, Output<U>> func) x 3 at async Task<OutputData<object>> Pulumi.Output<T>.Pulumi.IOutput.GetDataAsync() at async Task<object> Pulumi.Serialization.Serializer.SerializeAsync(string ctx, object prop, bool keepResources, bool keepOutputValues) x 2 at async Task<ImmutableDictionary<string, object>> Pulumi.Serialization.Serializer.SerializeDictionaryAsync(string ctx, IDictionary dictionary, bool keepResources, bool keepOutputValues) at async Task<ImmutableDictionary<string, object>> Pulumi.Serialization.Serializer.SerializeInputArgsAsync(string ctx, InputArgs args, bool keepResources, bool keepOutputValues) at async Task<object> Pulumi.Serialization.Serializer.SerializeAsync(string ctx, object prop, bool keepResources, bool keepOutputValues) at async Task<ImmutableArray<object>> Pulumi.Serialization.Serializer.SerializeListAsync(string ctx, IList list, bool keepResources, bool keepOutputValues) at async Task<object> Pulumi.Serialization.Serializer.SerializeAsync(string ctx, object prop, bool keepResources, bool keepOutputValues) x 3 at async Task<ImmutableDictionary<string, object>> Pulumi.Serialization.Serializer.SerializeDictionaryAsync(string ctx, IDictionary dictionary, bool keepResources, bool keepOutputValues) at async Task<ImmutableDictionary<string, object>> Pulumi.Serialization.Serializer.SerializeInputArgsAsync(string ctx, InputArgs args, bool keepResources, bool keepOutputValues) at async Task<object> Pulumi.Serialization.Serializer.SerializeAsync(string ctx, object prop, bool keepResources, bool keepOutputValues) x 3 at async Task<ImmutableDictionary<string, object>> Pulumi.Serialization.Serializer.SerializeDictionaryAsync(string ctx, IDictionary dictionary, bool keepResources, bool keepOutputValues) at async Task<ImmutableDictionary<string, object>> Pulumi.Serialization.Serializer.SerializeInputArgsAsync(string ctx, InputArgs args, bool keepResources, bool keepOutputValues) at async Task<object> Pulumi.Serialization.Serializer.SerializeAsync(string ctx, object prop, bool keepResources, bool keepOutputValues) x 3 at async Task<ImmutableDictionary<string, object>> Pulumi.Serialization.Serializer.SerializeDictionaryAsync(string ctx, IDictionary dictionary, bool keepResources, bool keepOutputValues) at async Task<ImmutableDictionary<string, object>> Pulumi.Serialization.Serializer.SerializeInputArgsAsync(string ctx, InputArgs args, bool keepResources, bool keepOutputValues) at async Task<object> Pulumi.Serialization.Serializer.SerializeAsync(string ctx, object prop, bool keepResources, bool keepOutputValues) x 3 at async Task<RawSerializationResult> Pulumi.Deployment.SerializeFilteredPropertiesRawAsync(string label, IDictionary<string, object> args, Predicate<string> acceptKey, bool keepResources, bool keepOutputValues) at async Task<SerializationResult> Pulumi.Deployment.SerializeFilteredPropertiesAsync(string label, IDictionary<string, object> args, Predicate<string> acceptKey, bool keepResources, bool keepOutputValues) at async Task<PrepareResult> Pulumi.Deployment.PrepareResourceAsync(string label, Resource res, bool custom, bool remote, ResourceArgs args, ResourceOptions options) at async Task<(string urn, string id, Struct data, ImmutableDictionary<string, ImmutableHashSet<Resource>> dependencies)> Pulumi.Deployment.RegisterResourceAsync(Resource resource, bool remote, Func<string, Resource> newDependency, ResourceArgs args, ResourceOptions options) at async Task<(string urn, string id, Struct data, ImmutableDictionary<string, ImmutableHashSet<Resource>> dependencies)> Pulumi.Deployment.ReadOrRegisterResourceAsync(Resource resource, bool remote, Func<string, Resource> newDependency, ResourceArgs args, ResourceOptions options) at async Task Pulumi.Deployment.CompleteResourceAsync(Resource resource, bool remote, Func<string, Resource> newDependency, ResourceArgs args, ResourceOptions options, ImmutableDictionary<string, IOutputCompletionSource> completionSources) at async Task<T> Pulumi.Output<T>.GetValueAsync(T whenUnknown) at async Task<string> Pulumi.Deployment+EngineLogger.TryGetResourceUrnAsync(Resource resource))
@bored-oyster-3147 idk if that's clear to you but it doesn't seem clear to me
fairly vague
b
It is failing to retrieve azure credentials from config within your mocks. So there must be a way to provide that config to the test invocation
b
Why aren't the default mocks enough for this then? In the docs they are made to look like a silver bullet if you will.
b
If you have a problem with the docs you could open an issue in the docs repo… but the mocks you write are meant to be specific to your stack implementation. Your mocks need to provide the things that your stack needs, in order to imitate the API calls that will not be made while you are unit testing.
b
Alright that's okay, I'll be creating mocks then. I might create an issue but not now I'm too busy sadly enough