Search code examples
c#.netvisual-studio-2017microsoft-fakesshim

Using Microsoft Fakes to shim SerialPort object


I want to write a unit test for a class that makes calls to a System.IO.Ports.SerialPort object. I need to return canned/simulated data from a fake serial port. When I create a Fakes assembly for System.dll it generates a System.IO.Ports.Fakes.StubSerialPort but no System.IO.Ports.Fakes.ShimSerialPort. The StubSerialPort doesn't appear to contain many methods, mostly just constructors. I can't find any examples anywhere for doing this, so I'm posting here. I'm running VS 2017 Enterprise.


Solution

  • Microsoft's answer here.

    For performance reasons Fakes does not generate shims for all types by default in the System/mscorlib modules. To tell it to generate shims for "System.IO.Ports" you could set the following in mscorlib.fakes and system.fakes for your project:

    <ShimGeneration>
        <Add Namespace="System.IO.Ports!"/>
    </ShimGeneration>