0

I am having trouble finding faked Thread.Sleep in mscorlib.fakes library. I am following direction at http://www.codewrecks.com/blog/index.php/2012/04/27/using-shims-in-visual-studio-11-to-test-untestable-code/

http://msdn.microsoft.com/en-us/library/d00bd51t(v=vs.100).aspx shows Thread.Sleep is in mscorlib so I added its fake but System.Threading.Fakes namespace doesn't contain ShimThread nor StubThread. Thread is a sealed class but VS fake framework should be able to fake static method in sealed class.

2
  • Are you trying to fake the Thread class or the Thread.Sleep method? Why would you want to? Commented Apr 22, 2013 at 23:01
  • in my test i check if Thread.Sleep is being called when it shouldn't be. I am planning to add Assert.Fail() inside fake Thread.Sleep
    – user156144
    Commented Apr 22, 2013 at 23:33

2 Answers 2

8

This is very much possible. By default Fakes framework doesn't generate shims for most types (including types in System.Threading namespace) of mscorlib because Fakes framework itself makes use of mscorlib. So only a few of the types are shimmed, However, you can configure this behavior by changing the mscorlib.fakes file added in your project.

<Fakes xmlns="http://schemas.microsoft.com/fakes/2011/">
    <Assembly Name="mscorlib" Version="4.0.0.0"/>
    <ShimGeneration>
        <Add Namespace="System.Threading!"/>
    </ShimGeneration>
</Fakes>

Now build the test project and you can see shims for types in the System.Threading namespace, including ShimThread.SleepInt32.

Read more about the .fakes xml file on this page

5
  • Thank you! Unfortunately, this only got me halfway there. I did this, and it generated Stubs but not Shims. Any idea what might be going wrong? Commented Dec 12, 2013 at 21:45
  • @Dustin Are you sure you used <ShimGeneration> in the .fakes file? Because that should only generate shims and not stubs. I verify that it has worked for me and I have actually recently used it to intercept Thread.Sleep in multiple test cases.
    – Varun K
    Commented Dec 14, 2013 at 8:50
  • To be more specific, I was attempting to shim System.ServiceModel.Channels. I definitely had <ShimGeneration> in the config file. I also tried <StubGeneration>. I ended up working around the issue though, so this problem is still unresolved. Commented Dec 14, 2013 at 14:45
  • this works, but do remember to rebuild your project after adding the code in order for the shim to show up in intellisense
    – Batavia
    Commented Aug 2, 2016 at 12:16
  • It didn't work for me.When I build project compiler complains about AsyncLocal<> doesn't exist in the namespace 'System.Threading'
    – erhan355
    Commented Mar 28, 2018 at 11:03
3

This is because the Shim framework cannot fake all .Net BCL classes in mscrolib and System, see: MSDN.

Unfortunately I couldn't find a list of types that aren't supported. But it seems, primarily types that are not pure CLR classes or need OS functionality (Thread, FileSystemWatcher, ...) are not supported at this time.

Not the answer you're looking for? Browse other questions tagged or ask your own question.