pulumi/sdk/dotnet/Pulumi.Tests/PulumiTest.cs
Mikhail Shilkov a95a4d1195
Unit testing in .NET (#3696)
Mock-based testing in .NET
2020-03-11 23:10:01 +01:00

41 lines
1.1 KiB
C#

// Copyright 2016-2019, Pulumi Corporation
using System;
using System.Threading.Tasks;
using Moq;
namespace Pulumi.Tests
{
public abstract class PulumiTest
{
private static Task Run(Action action, bool dryRun)
=> Run(() =>
{
action();
return Task.CompletedTask;
}, dryRun);
private static async Task Run(Func<Task> func, bool dryRun)
{
var mock = new Mock<IDeployment>(MockBehavior.Strict);
mock.Setup(d => d.IsDryRun).Returns(dryRun);
Deployment.Instance = mock.Object;
await func().ConfigureAwait(false);
Deployment.Instance = null!;
}
protected static Task RunInPreview(Action action)
=> Run(action, dryRun: true);
protected static Task RunInNormal(Action action)
=> Run(action, dryRun: false);
protected static Task RunInPreview(Func<Task> func)
=> Run(func, dryRun: true);
protected static Task RunInNormal(Func<Task> func)
=> Run(func, dryRun: false);
}
}