Skip to content

Instantly share code, notes, and snippets.

@hatelove
Created March 7, 2012 03:48
Show Gist options
  • Save hatelove/1990816 to your computer and use it in GitHub Desktop.
Save hatelove/1990816 to your computer and use it in GitHub Desktop.
//第二版
public class Target
{
public delegate int MyDelegate();
private MyDelegate _myDelegate;
public MyDelegate GetsMod
{
get
{
if (_myDelegate == null)
{
_myDelegate = new DelegateSource().DelegateOne;
}
return _myDelegate;
}
set
{
_myDelegate = value;
}
}
public int MyMethod(int original)
{
var mod = this.GetsMod();
return original % mod;
}
}
public class DelegateSource
{
public int DelegateOne()
{
var result = GetMyValue();
return result;
}
private int GetMyValue()
{
return 2;
}
}
//測試
/// <summary>
///MyMethod 的測試
///</summary>
[TestMethod()]
public void MyMethodTest()
{
Target target = new Target();
int original = 3;
int expected = 1;
int actual;
actual = target.MyMethod(original);
Assert.AreEqual(expected, actual);
}
/// <summary>
///MyMethod 的測試
///</summary>
[TestMethod()]
public void MyMethodTest_2()
{
Target target = new Target();
int original = 2;
int expected = 0;
int actual;
actual = target.MyMethod(original);
Assert.AreEqual(expected, actual);
}
/// <summary>
///MyMethod 的測試
///</summary>
[TestMethod()]
public void MyMethodTest_mockDelegate()
{
Target target = new Target() { GetsMod = () => { return 5; } };
int original = 7;
int expected = 2;
int actual;
actual = target.MyMethod(original);
Assert.AreEqual(expected, actual);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment