NSubstitute cannot blink on extension methods according to Sriram's comment, but you can still pass a laughed argument to the extension method.
In this case, the Random class has virtual methods, so we can spoof it directly using the NSubstitute tools and other tools based on a dynamic proxy server. (For NSubstitute in particular, we need to be very careful about mocking classes. Please read the warning in the documentation .)
public static class RandomExtensions { public static IEnumerable<int> NextInt32s(this System.Random random, int neededValuesNumber, int minInclusive, int maxExclusive) { } } public class RandomExtensionsTests { [Test] public void Select() { const int min = 0, max = 10; var randomizer = Substitute.For<Random>(); randomizer.Next(min, max).Returns(1, 2, 3); var result = randomizer.NextInt32s(3, 0, 10).ToArray(); Assert.AreEqual(new[] {1, 2, 3}, result); } }
David Tchepak
source share