Just had a crack at writing unit tests with the do...
# dotnet
Just had a crack at writing unit tests with the dotnet SDK and it is pretty awesome so far
Iā€™m working on examples and docs for this, would love to know more about your experience/examples!
My experience is very good so far. I think the two things that I wish they were already part of the default experience is
to return all the resources without a need from me to do mocking so I ended up with having this little helper to return resources:
Copy code
public static async Task<ImmutableArray<Resource>> TestAsync<TStack>(
            TestOptions options = null)
            where TStack : Stack, new()
            var mock = new Mock<IMocks>();

            mock.Setup(m => m.NewResourceAsync(It.IsAny<string>(), It.IsAny<string>(),
                    It.IsAny<ImmutableDictionary<string, object>>(), It.IsAny<string>(), It.IsAny<string>()))
                .ReturnsAsync((string resource, string name, ImmutableDictionary<string, object> inputs, string provider,
                    string id) => (name + "-test", inputs));
            var resources = await Deployment.TestAsync<MyStack>(mock.Object);
            return resources;
The other one is to extract the value of
Copy code
public static class OutputExtensions
        public static T GetValue<T>(this Output<T> output)
            T result=default;
            output.Apply(x => result = x);
            return result;
so to validate that a resource group has tags of product on it, my test looks like this
Copy code
public async Task AllResourceGroups_Should_Have_ProductName_Tag()
    var resources = await TestAsync<MyStack>();
    var resourceGroups = resources.OfType<Pulumi.Azure.Core.ResourceGroup>();
    resourceGroups.ShouldAllBe(rg =>rg.Tags.GetValue().ContainsKey("productname"));
Right, I have two helper methods very similar to yours šŸ™‚
We may ship a default implementation of mocks once we find that it fits 80+% of scenarios. We need to think of a way to expose the extraction of outputs in a way that would not invite people to do the same in their Pulumi programs (not tests).
is inspired from the your
that was part of the unit tests pull request so credit to you šŸ˜‰
I see, your concern is very valid.
The other thing that stuck me when I was starting is I was under the impression that there will be a nuget package
or something like that but then I check the PR and found that it is in the same package