Skip to content

Instantly share code, notes, and snippets.

@mattwcole
Created February 3, 2016 21:48
Show Gist options
  • Save mattwcole/e088a25cb84b041399a0 to your computer and use it in GitHub Desktop.
Save mattwcole/e088a25cb84b041399a0 to your computer and use it in GitHub Desktop.
using System;
using Moq;
namespace MoqDemo
{
public class Program
{
public static void Main()
{
var mockSqlUtility = new Mock<SqlUtility> {CallBase = true};
// Setup the only method that you want to mock (must be virtual). All other
// methods will use the real implementation. Try commenting this line out,
// and see it goes back to using the real method.
mockSqlUtility.Setup(m => m.Garply())
.Callback(() => Console.WriteLine("CALLED MOCKED GARPLY"));
var app = new Application(mockSqlUtility.Object);
app.Run();
}
}
public class Application
{
private readonly ISqlUtility _sqlUtility;
public Application(ISqlUtility sqlUtility)
{
_sqlUtility = sqlUtility;
}
public void Run()
{
_sqlUtility.Foo<string>();
_sqlUtility.Bar<int>();
_sqlUtility.Baz("baz");
_sqlUtility.Qux("qux");
_sqlUtility.Quux();
_sqlUtility.Garply();
_sqlUtility.Waldo();
}
}
public class SqlUtility : ISqlUtility
{
public T Foo<T>()
{
Console.WriteLine("Called real Foo");
return default(T);
}
public T Bar<T>()
{
Console.WriteLine("Called real Bar");
return default(T);
}
public void Baz<T>(T value)
{
Console.WriteLine("Called real Baz");
}
public void Qux<T>(T value)
{
Console.WriteLine("Called real Qux");
}
public void Quux()
{
Console.WriteLine("Called real Quux");
}
public virtual void Garply()
{
Console.WriteLine("Called real Garply");
}
public void Waldo()
{
Console.WriteLine("Called real Waldo");
}
}
public interface ISqlUtility
{
T Foo<T>();
T Bar<T>();
void Baz<T>(T value);
void Qux<T>(T value);
void Quux();
void Garply();
void Waldo();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment