Search code examples
c#unit-testingmoqmicrosoft-fakes

Unable to shim WebException with Microsoft.QualityTools.Testing.Fakes.Shims.ShimNotSupportedException


I am attempting to test some code outside of my control that checks for a WebException and reacts appropriately. I am trying to generate a shim for WebException, but keep receiving a

Microsoft.QualityTools.Testing.Fakes.Shims.ShimNotSupportedException.

Here is the source of the test:

[TestMethod]
public async Task Search_WebException404Ignored()
{
    using (ShimsContext.Create())
    {
        mockApiSettings
            .Setup(p => p.ListApiUrl)
            .Returns("http://fakeUrl/?value={0}{1}");

        var exception = new ShimWebException
        {
            ResponseGet = () => new ShimHttpWebResponse {StatusCodeGet = () => HttpStatusCode.NotFound},
        };

        mockServiceProxy
            .Setup(m => m.CallServiceAsync(It.IsAny<Uri>(), RequestMethodType.GET, null))
            .ThrowsAsync(exception);

        var items = await service.Search("ListName_0", new[] {"ListValue_0", "ListValue_0"});
    }
}

I have updated the System.fakes file as so:

<Fakes xmlns="http://schemas.microsoft.com/fakes/2011/" Diagnostic="true">
  <Assembly Name="System" Version="4.0.0.0"/>
  <StubGeneration>
    <Clear/>
  </StubGeneration>
  <ShimGeneration>
    <Clear/>
    <Add FullName="System.Net.WebRequest!"/>
    <Add FullName="System.Net.HttpWebRequest!"/>
    <Add FullName="System.Net.HttpWebResponse!"/>
    <Add FullName="System.Net.WebException!"/>
  </ShimGeneration>
</Fakes>

Doesn't seem to help. Could it be because I'm also using MOQ?

The test project does not use a .testSettings file as mentioned in other posts. Using 64 bit for source and unit tests.

I am using Visual Studio 2015 Enterprise and Microsoft.QualityTools.Testing.Fakes 12.0.0.0. No warnings show in the output for shim generation for those items.


Solution

  • I had a similar error when my unit test project is referencing Microsoft.QualityTools.Testing.Fakes 12.0 and running some tests with Shims within VS2015.

    I managed to fix the issue by updating that DLL file to the one from VS2015 Public Assemblies folder, in my case it is %PROGRAMFILES(x86)%\Microsoft Visual Studio 14.0\Common7\IDE\PublicAssemblies\Microsoft.VisualStudio.QualityTools.Testing.Fakes.dll.

    I didn't have to regenerate my System.4.0.0.0.Fakes reference after updating the DLL file, and I have exactly the same System.fakes file as you including the ! in the FullName.

    Note: It's also a good idea to check the version of your Microsoft.VisualStudio.QualityTools.UnitTestFramework, and update it to version 14.0 as well.