Skip to content

Commit

Permalink
#4 Hopefully root out the last remnants of Microsoft-related test fra…
Browse files Browse the repository at this point in the history
…meworks

The following line makes it so that tests are run sequentially.
This seems to be necessary.
If I don't have it, tests seem to fail randomly.

I suspect it's related to the IL being rewritten.
Should probably look further into it.
  • Loading branch information
sguldmund committed Jan 12, 2024
1 parent 353d16f commit 3407527
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 18 deletions.
15 changes: 10 additions & 5 deletions test/Pose.Tests/IL/MethodRewriterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,34 +11,39 @@ namespace Pose.Tests
{
public class MethodRewriterTests
{
private class ClassWithStaticMethod
{
public static string Now { get; } = "?";
}

[Fact]
public void Can_rewrite_static_method()
{
// Arrange
var methodInfo = typeof(DateTime).GetMethod("get_Now");
var methodInfo = typeof(ClassWithStaticMethod).GetMethod("get_Now");
var methodRewriter = MethodRewriter.CreateRewriter(methodInfo, false);

// Act
var dynamicMethod = methodRewriter.Rewrite() as DynamicMethod;
var func = dynamicMethod.CreateDelegate(typeof(Func<DateTime>));
var func = dynamicMethod.CreateDelegate(typeof(Func<string>));

// Assert
func.DynamicInvoke().As<DateTime>().Should().BeCloseTo(DateTime.Now, TimeSpan.FromSeconds(10));
func.DynamicInvoke().As<string>().Should().BeEquivalentTo("?");
}

[Fact]
public void Cannot_rewrite_method_in_CoreLib()
{
// Arrange
var methodInfo = typeof(DateTime).GetMethod("get_Now");
var methodInfo = typeof(DateTime).GetMethod("get_UtcNow");
var methodRewriter = MethodRewriter.CreateRewriter(methodInfo, false);

// Act
var dynamicMethod = methodRewriter.Rewrite() as DynamicMethod;
var func = dynamicMethod.CreateDelegate(typeof(Func<DateTime>));

// Assert
func.DynamicInvoke().As<DateTime>().Should().BeCloseTo(DateTime.Now, TimeSpan.FromSeconds(10));
func.DynamicInvoke().As<DateTime>().Should().BeCloseTo(DateTime.UtcNow, TimeSpan.FromSeconds(10));
}

[Fact]
Expand Down
2 changes: 1 addition & 1 deletion test/Pose.Tests/Pose.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<PackageReference Include="MSTest.TestAdapter" Version="1.1.18" />
<PackageReference Include="MSTest.TestFramework" Version="1.1.18" />
<PackageReference Include="FluentAssertions" Version="6.12.0" />
<PackageReference Include="xunit" Version="2.6.5" />
<PackageReference Include="xunit" Version="2.3.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" />
</ItemGroup>

Expand Down
26 changes: 14 additions & 12 deletions test/Pose.Tests/ShimTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@
using System.Threading;
using FluentAssertions;
using Pose.Exceptions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Xunit;

// ReSharper disable RedundantLambdaParameterType
// ReSharper disable PossibleNullReferenceException

// See: https://stackoverflow.com/a/34876963
[assembly: CollectionBehavior(DisableTestParallelization = true)]

namespace Pose.Tests
{
public class ShimTests
Expand Down Expand Up @@ -170,7 +172,7 @@ public void TestReplacePropertySetterAction()
setterExecuted.Should().BeTrue(because: "the shim was executed");
}

[TestMethod]
[Fact]
public void Can_shim_static_property()
{
// Arrange
Expand Down Expand Up @@ -217,7 +219,7 @@ public string GetString()
}
}

[TestMethod]
[Fact]
public void Can_shim_instance_property_getter()
{
// Arrange
Expand All @@ -233,7 +235,7 @@ public void Can_shim_instance_property_getter()
dt.Should().BeEquivalentTo("Hello", because: "that is what the shim is configured to return");
}

[TestMethod]
[Fact]
public void Can_shim_instance_property_setter()
{
// Arrange
Expand All @@ -250,7 +252,7 @@ public void Can_shim_instance_property_setter()
wasCalled.Should().BeTrue(because: "the shim has been called");
}

[TestMethod]
[Fact]
public void Can_shim_static_property_getter()
{
// Arrange
Expand All @@ -265,7 +267,7 @@ public void Can_shim_static_property_getter()
dt.Should().BeEquivalentTo("Hello", because: "that is what the shim is configured to return");
}

[TestMethod]
[Fact]
public void Can_shim_static_property_setter()
{
// Arrange
Expand All @@ -279,7 +281,7 @@ public void Can_shim_static_property_setter()
wasCalled.Should().BeTrue(because: "the shim has been called");
}

[TestMethod]
[Fact]
public void Can_shim_instance_method()
{
// Arrange
Expand All @@ -299,7 +301,7 @@ public void Can_shim_instance_method()
dt.Should().BeEquivalentTo("String", because: "that is what the shim is configured to return");
}

[TestMethod]
[Fact]
public void Can_shim_instance_method_of_value_type()
{
// Arrange
Expand All @@ -318,7 +320,7 @@ public void Can_shim_instance_method_of_value_type()
dt.Should().BeEquivalentTo("String", because: "that is what the shim is configured to return");
}

[TestMethod]
[Fact]
public void Can_shim_instance_method_of_specific_instance()
{
// Arrange
Expand All @@ -337,7 +339,7 @@ public void Can_shim_instance_method_of_specific_instance()
dt.Should().BeEquivalentTo("String", because: "that is what the shim is configured to return");
}

[TestMethod]
[Fact]
public void Shims_only_the_method_of_the_specified_instance()
{
// Arrange
Expand All @@ -362,7 +364,7 @@ public void Shims_only_the_method_of_the_specified_instance()
responseFromNonShimmedInstance.Should().BeEquivalentTo("!", because: "that is what the instance returns by default");
}

[TestMethod]
[Fact]
public void Can_shim_static_method()
{
// Arrange
Expand All @@ -380,7 +382,7 @@ public void Can_shim_static_method()
dt.Should().BeEquivalentTo("String", because: "that is what the shim is configured to return");
}

[TestMethod]
[Fact]
public void Can_shim_constructor()
{
// Arrange
Expand Down

0 comments on commit 3407527

Please sign in to comment.