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
}
}