namespace PlayingWithMoles
{
// , -
public interface IFoo
{
string SomeMethod();
}
// - , IFoo
public class FooConsumer
{
// ""
public FooConsumer(IFoo foo)
{
this .foo = foo;
}
// , - IFoo
public void DoStuff()
{
var someResults = foo.SomeMethod();
Console .WriteLine( "Doing stuff. IFoo.SomeMethod results: {0}" , someResults);
}
private readonly IFoo foo;
}
}
* This source code was highlighted with Source Code Highlighter .
[TestClass()]
public class FooConsumerTest
{
class FooTester : IFoo
{
public string SomeMethod()
{
return "Test string" ;
}
}
[TestMethod]
public void DoStuffTest()
{
IFoo foo = new FooTester();
FooConsumer target = new FooConsumer(foo);
target.DoStuff();
}
}
* This source code was highlighted with Source Code Highlighter .
namespace PlayingWithMoles.Moles
{
public class SIFoo
: Microsoft.Moles.Framework.Stubs.StubBase
, IFoo
{
string IFoo.SomeMethod()
{
var sh = this .SomeMethod;
if (sh != null )
return sh.Invoke();
else
{
// Behavior-,
}
}
// Sets the stub of IFoo.SomeMethod
public Func< string > SomeMethod;
}
}
* This source code was highlighted with Source Code Highlighter .
[TestMethod]
public void DoStuffTestWithStubs()
{
var fooStub = new PlayingWithMoles.Moles.SIFoo();
fooStub.SomeMethod = () => "Hello, Stub!" ;
var target = new FooConsumer(fooStub);
target.DoStuff();
}
* This source code was highlighted with Source Code Highlighter .
namespace PlayingWithMoles
{
// Foo,
// , IFoo
public class Foo
{
public string SomeMethod()
{
return "Hello, from non-virtual method." ;
}
}
// - , Foo
public class FooConsumer
{
// Foo
public FooConsumer(Foo foo)
{
this .foo = foo;
}
// , - Foo
public void DoStuff()
{
var someResults = foo.SomeMethod();
Console .WriteLine( "Doing stuff. Foo.SomeMethod results: {0}" ,
someResults);
}
private readonly Foo foo;
}
}
* This source code was highlighted with Source Code Highlighter .
[TestMethod]
[HostType( "Moles" )]
public void DoStuffTestWithMoles()
{
var fooMole = new PlayingWithMoles.Moles.MFoo();
fooMole.SomeMethod = () => "Hello, Mole!" ;
var target = new FooConsumer(fooMole);
target.DoStuff();
}
* This source code was highlighted with Source Code Highlighter .
public class WebRequester
{
public void RequestWebPage( string url)
{
var request = WebRequest.Create(url);
var response = request.GetResponse();
Console .WriteLine( "Sync version. URL: {0}, Response content length: {1}" ,
url, response.ContentLength);
}
}
* This source code was highlighted with Source Code Highlighter .
[TestMethod]
[HostType( "Moles" )]
public void RequestWebPageTest()
{
var mole = new System.Net.Moles.MHttpWebResponse();
mole.ContentLengthGet = () => 5;
// URL-.
// , :
//System.Net.Moles.MHttpWebRequest.AllInstances.GetResponse = (r) =>
// {
// if (r.RequestUri == new Uri("http://rsdn.ru"))
// return mole;
// return r.GetResponse();
// };
//
System.Net.Moles.MHttpWebRequest.AllInstances.GetResponse = (r) => mole;
WebRequester target = new WebRequester();
target.RequestWebPage( "http://rsdn.ru" );
}
* This source code was highlighted with Source Code Highlighter .
public void RequestWebPageAsync( string url)
{
var waiter = new ManualResetEvent( false );
var request = WebRequest.Create(url);
request.BeginGetResponse(
ar=>
{
var response = request.EndGetResponse(ar);
Console .WriteLine( "Async version. URL: {0}, Response content length: {1}" ,
url, response.ContentLength);
waiter.Set();
}, null );
waiter.WaitOne();
}
* This source code was highlighted with Source Code Highlighter .
[TestMethod]
[HostType( "Moles" )]
public void RequestWebPageAsyncTest()
{
var mole = new System.Net.Moles.MHttpWebResponse();
mole.ContentLengthGet = () => 5;
// ,
//
Action action = () => Thread.Sleep(500);
// , ,
//
// - (-,
// ).
ThreadPool.SetMinThreads(3, 3);
System.Net.Moles.MHttpWebRequest.AllInstances.BeginGetResponseAsyncCallbackObject =
(r, a, iar) => action.BeginInvoke(a, iar);
System.Net.Moles.MHttpWebRequest.AllInstances.EndGetResponseIAsyncResult =
(r, iar) => { action.EndInvoke(iar); return mole; };
WebRequester target = new WebRequester();
target.RequestWebPageAsync(http: //rsdn.ru);
}
* This source code was highlighted with Source Code Highlighter .
Source: https://habr.com/ru/post/109891/