https://pulumi.com logo
#dotnet
Title
f

flat-mouse-20634

03/22/2020, 4:03 AM
Just had a crack at writing unit tests with the dotnet SDK and it is pretty awesome so far
t

tall-librarian-49374

03/22/2020, 10:09 AM
Iā€™m working on examples and docs for this, would love to know more about your experience/examples!
f

flat-mouse-20634

03/24/2020, 9:07 AM
My experience is very good so far. I think the two things that I wish they were already part of the default experience is
Deployment.TestAsync
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
Output
variables:
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
[Fact]
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"));
}
t

tall-librarian-49374

03/24/2020, 9:25 AM
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).
f

flat-mouse-20634

03/24/2020, 9:30 AM
My
TestStack
is inspired from the your
TestProperty
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
Pulumi.Tests
or something like that but then I check the PR and found that it is in the same package