Visual Studio Shims and Fakes

Microsoft Visual Studio 2013 includes Fakes (Premium and above) to isolate existing code. This involves intercepting calls to the framework and rerouting them to Shims. These Shims are created by right clicking a reference and selecting “Add Fakes Assembly”. But sometimes a little manual override is needed to get things working.

[TestMethod]
public void Stub_environment_and_dns() {
   // arrange
   using (ShimsContext.Create()) {
      System.Fakes.ShimEnvironment.MachineNameGet = () => "hello";
      System.Net.Fakes.ShimDns.GetHostName = () => "world";

   // act
      var result = string.Format("{0} {1}",
         System.Environment.MachineName,
         System.Net.Dns.GetHostName());

   // assert
      Assert.AreEqual("hello world", result);
   }
}

To get the above code to work edit the mscorlib.fakes and System.fakes files. We need to instruct the generator to create fakes for System.Net.Dns and System.Environment.

<Fakes xmlns="http://schemas.microsoft.com/fakes/2011/">
   <Assembly Name="System" Version="4.0.0.0" />
   <ShimGeneration>
      <Add FullName="System.Net.Dns"/>
   </ShimGeneration>
</Fakes>
<Fakes xmlns="http://schemas.microsoft.com/fakes/2011/">
   <Assembly Name="mscorlib" Version="4.0.0.0"/>
   <ShimGeneration>
      <Add FullName="System.Environment"/>
   </ShimGeneration>
</Fakes>

Still the build output contains warnings:

1>mscorlib.fakes : warning : Some fakes could not be generated. For complete details, set Diagnostic attribute of the Fakes element in this file to ‘true’ and rebuild the project.
1>System.fakes : warning : Some fakes could not be generated. For complete details, set Diagnostic attribute of the Fakes element in this file to ‘true’ and rebuild the project.

This makes me reserved in using this for my projects.

Posted in Development | Tagged , | 2 Comments

Week roundup

Last week recap and links:

Image courtesy of kanate / FreeDigitalPhotos.net

Image courtesy of kanate / FreeDigitalPhotos.net

What are your best reads this week? Leave them in the comments below.

Posted in Uncategorized | Tagged , , , | Leave a comment

Windows 8.1 Reliability Monitor

Thanks to Scott Hanselman’s tool list I started using the Reliability Monitor. It is located in Control Panel\All Control Panel Items\Action Center\Reliability Monitor. Below is a screenshot of the report on my computer.

reliability_report

Before the only days my computer had no critical events (red icon) was in the weekends. When the computer would not be turned on. There was definitely something wrong with my machine.

After fixing the issue with the video driver (on a Wednesday) my machine stopped producing critical events. Not that I notice any difference in performance, but the feeling is great!

Posted in Tooling | Tagged | Leave a comment

SQLite WARNING: Type mapping failed

The log of our product started showing SQLite warnings. We ignored them to focus on the release. Now it was time to repro and fix the warnings.

After reading the verbose log we found the issue. Unknown datatypes are allowed when creating SQLite database, but log a warning when the check database routine would call GetSchema.

WARNING: Type mapping failed, returning default type Object for name “LARGEINT”.
WARNING: Type mapping failed, returning default name “” for type Object.

See the repro code below. System.Data.SQLite can be downloaded from here.

static void Main(string[] args) {
    // Trace to see SQLite warning
    Trace.Listeners.Add(new ConsoleTraceListener());
    // Use in-memory database
    var connectionString = "Data Source=:memory:;Version=3;";
    using(var connection = new SQLiteConnection(connectionString)) {
        connection.Open();
        // Create table with unknown datatype column
        using (var createTableOne = connection.CreateCommand()) {
            createTableOne.CommandText = 
                "CREATE TABLE One (Id LARGEINT NOT NULL);";
            createTableOne.ExecuteNonQuery();
        }
        // Table is created, no exceptions, only warning on GetSchema
        var schema = connection.GetSchema("Columns", null);
        var result = schema.Select("TABLE_NAME = 'One'");
        if (result.Length > 0) Console.WriteLine("One exists");
        else Console.WriteLine("One not exists");
    }
    Console.ReadLine();
}

We edited the create database scripts to use known SQLite types and the issue was resolved.

Posted in Development | Tagged , | Leave a comment

Week roundup

Video edition:

Image courtesy of kanate / FreeDigitalPhotos.net

Image courtesy of kanate / FreeDigitalPhotos.net

What are your best reads this week? Leave them in the comments below.

Posted in Uncategorized | Tagged , , | Leave a comment