0

I like using the Microsoft Fakes framework for intercepting code that I have no control over such as .net Framework objects such as things that live in System. This also removes the need to write wrapper code for these type of objects. However, there times working with this framework that leaves me quite frustrated. I am using the following namespace System.Text.RegularExpressions and trying to setup Shims for the Regex Object. However, it is not available and it is not clear why. In the past, I have also used the Moles and have encountered similar scenarios where something is not moled but not sure why. I often have to write a lot of code to get around this problem, however I would like to avoid this if possible.

If anyone has a solution to this problem, please provide a solution. I would like to avoid changing mocking and testing frameworks.

1 Answer 1

1

I got this to work.

    [TestMethod]
    public void TestRegex()
    {
        using (ShimsContext.Create())
        {
            System.Text.RegularExpressions.Fakes.ShimRegex.IsMatchStringString = (s, s1) => true;
            bool result = Regex.IsMatch("TestString", "NoMatch");
            Console.WriteLine("regex result: " + result);
            Assert.IsFalse(result);
        }
    }

Needed to also modify the "System.fakes" file. I find most frustrations with the Fakes framework can be resolved by editing these configuration files. Not sure why it need to be explicitly added in this case.

<Fakes xmlns="http://schemas.microsoft.com/fakes/2011/">
<Assembly Name="System" Version="4.0.0.0"/>
<ShimGeneration>
    <Add FullName="System.Text.RegularExpressions.Regex"/>
</ShimGeneration>
</Fakes>
1
  • This got my immediate need taken care of. It is almost like it is not recursive for the nested types. I still have some types that are not showing up but this takes care of more than half of my problem items. thx.
    – user2932053
    Dec 15, 2015 at 13:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.