Skip to content

Instantly share code, notes, and snippets.

@podhmo
Created February 2, 2014 11:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save podhmo/8767093 to your computer and use it in GitHub Desktop.
Save podhmo/8767093 to your computer and use it in GitHub Desktop.
using NUnit.Framework;
using System;
namespace mockSample
{
public interface IFoo
{
bool DoSomething (string message);
bool TryParse (string message, out string outstring);
int GetCount ();
}
public class Foo : IFoo
{
public bool DoSomething (string message)
{
throw new NotImplementedException ();
}
public bool TryParse (string message, out string outstring)
{
throw new NotImplementedException ();
}
public int GetCount ()
{
throw new NotImplementedException ();
}
}
[TestFixture ()]
public class QuickStart
{
[Test ()]
public void TestCase1 ()
{
var moq = new Moq.Mock<IFoo> ();
moq.Setup (foo => foo.DoSomething ("ping")).Returns (true);
Assert.IsTrue (moq.Object.DoSomething ("ping"));
}
[Test ()]
public void TestCase2 ()
{
var moq = new Moq.Mock<IFoo> ();
var outstring = "ack";
moq.Setup (foo => foo.TryParse ("ping", out outstring)).Returns (true);
Assert.IsTrue (moq.Object.TryParse ("ping", out outstring));
}
[Test (),ExpectedException (typeof(InvalidOperationException))]
public void TestCase3 ()
{
var moq = new Moq.Mock<IFoo> ();
moq.Setup (x => x.DoSomething ("reset")).Throws <InvalidOperationException> ();
moq.Object.DoSomething ("reset");
}
[Test ()]
public void TestCase4 ()
{
var items = new int[]{ 0, 1, 2 };
var moq = new Moq.Mock<IFoo> ();
moq.Setup (x => x.GetCount ()).Returns (() => items.Length);
Assert.AreEqual (3, moq.Object.GetCount ());
}
[Test ()]
public void TestCase5 ()
{
var i = 0;
var moq = new Moq.Mock<IFoo> ();
moq.Setup (x => x.GetCount ()).Returns (() => i).Callback (() => i++);
Assert.AreEqual (0, moq.Object.GetCount ());
Assert.AreEqual (1, moq.Object.GetCount ());
Assert.AreEqual (2, moq.Object.GetCount ());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment