Daily Usage Issues

NCrunch fails for tests using VS2012 Fakes

Started by BKStrelioff on 6,725 views

For example, the following tests pass when run under NUnit, but fail under NCrunch:

#region usings

using System;
using System.Fakes;
using System.IO;
using System.Net;
using System.Net.Fakes;
using System.Text;

using Microsoft.QualityTools.Testing.Fakes;
using Microsoft.VisualStudio.TestTools.UnitTesting;

#endregion

namespace Bks.Framework.Common.Tests
{

/// <summary>
/// </summary>
[ TestClass ]
public class DnsResolverTest
{
#region Instance Public Methods

/// <summary>
/// </summary>
[ TestMethod ]
public void TestMetho01 ( )
{
using ( ShimsContext.Create ( ) )
{
ShimUri.ConstructorString = ( uri, temp ) => // MCrunch says Microsoft.QualityTools.Testing.Fakes.Shims.ShimNotSupportedException: System.Uri
{
throw new NotImplementedException ( "Not valid in a unit test" );
};

var request = WebRequest.CreateHttp ( "http://msdn.microsoft.com/en-us/library/hh549176.aspx#ShimsContext" );
Assert.IsNotNull ( request );
var response = request.GetResponse ( );
Assert.IsNotNull ( response );
}
}


/// <summary>
/// </summary>
[ TestMethod ]
public void TestMethod1 ( )
{
using ( ShimsContext.Create ( ) )
{
var responseShim = new ShimHttpWebResponse ( );
var requestShim = new ShimHttpWebRequest ( );

ShimWebRequest.CreateString = ( uri ) => requestShim.Instance; // NCrunch says Microsoft.QualityTools.Testing.Fakes.Shims.ShimNotSupportedException:
requestShim.GetRequestStream = ( ) => new MemoryStream ( );
requestShim.GetResponse = ( ) => responseShim.Instance;
responseShim.GetResponseStream = ( ) => new MemoryStream ( Encoding.ASCII.GetBytes ( "Hello World" ) );
Assert.AreEqual ( "Hello World", GetWebData ( "http://www.bing.com" ) );
}
}

#endregion

#region Private Methods

private string GetWebData ( string baseUrl )
{
var myRequest = ( HttpWebRequest ) WebRequest.Create ( "http://msdn.microsoft.com/en-us/library/hh549176.aspx#ShimsContext" );

var response = myRequest.GetResponse ( );
var responseStream = response.GetResponseStream ( );
var responseReader = new StreamReader ( responseStream );
var result = responseReader.ReadToEnd ( );
return result;
}

#endregion
}

}
Right now, NCrunch doesn't have support for MS Fakes Shims. These shims require a specialised profiler to be loaded into the test environment, which NCrunch unfortunately doesn't handle yet.

Plans exist to introduce support for Fakes as part of NCrunch V2 (due in Q4 this year) - http://www.ncrunch.net/support/frameworks

Edited

Post a reply

Log in to reply.