From ada9c53b047fb9ab9ae80edf675f88c8882ff550 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B8ren=20Guldmund?= Date: Sat, 13 Jan 2024 20:19:03 +0100 Subject: [PATCH] #4 Add missing tests for shimming setters on value types --- test/Pose.Tests/ShimTests.cs | 43 +++++++++++++++++++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-) diff --git a/test/Pose.Tests/ShimTests.cs b/test/Pose.Tests/ShimTests.cs index e225473..7e411eb 100644 --- a/test/Pose.Tests/ShimTests.cs +++ b/test/Pose.Tests/ShimTests.cs @@ -551,9 +551,50 @@ public void Can_shim_property_setter_of_specific_instance() public class ValueTypes { + private struct Instance + { + public string Text { get; set; } + } - } + [Fact] + public void Can_shim_property_setter_of_any_instance() + { + // Arrange + var invocationCount = 0; + var shim = Shim + .Replace(() => Is.A().Text, true) + .With((ref Instance @this, string prop) => { invocationCount++; }); + + // Pre-act assert + invocationCount.Should().Be(0, because: "the shim has not been called yet"); + + // Act + PoseContext.Isolate( + () => + { + var instance1 = new Instance { Text = "Hello" }; + var instance2 = new Instance { Text = "Hello" }; + }, shim); + + // Assert + invocationCount.Should().Be(2, because: "the shim was invoked 2 times"); + } + + [Fact] + public void Cannot_shim_property_setter_of_specific_instance() + { + // Arrange + var instance = new Instance(); + Action act = () => Shim + .Replace(() => instance.Text, setter: true) + .With((ref Instance @this) => string.Empty); + + // Assert + act.Should().Throw(because: "instance methods on specific value type instances cannot be replaced"); + } + } + public class SealedTypes { private sealed class SealedClass