Skip to content

Instantly share code, notes, and snippets.

@hatelove
Created March 7, 2012 04:04
Show Gist options
  • Save hatelove/1990870 to your computer and use it in GitHub Desktop.
Save hatelove/1990870 to your computer and use it in GitHub Desktop.
//第三版
public class Target
{
public delegate int MyDelegate();
private MyDelegate _myDelegate = new DelegateSource().DelegateOne;
public int MyMethod(int original)
{
var mod = _myDelegate();
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_mockPrivateDelegate()
{
Target_Accessor target = new Target_Accessor();
//assign private的_myDelegate,則會走mock object function
target._myDelegate = () => { return 5; };
int original = 7;
int expected = 2;
int actual;
//7%5=2;
actual = target.MyMethod(original);
Assert.AreEqual(expected, actual);
}
/// <summary>
///MyMethod 的測試
///</summary>
[TestMethod()]
public void MyMethodTest_WithDelegateSource()
{
Target_Accessor target = new Target_Accessor();
//不assign private的_myDelegate,則會走原本的new DelegateSource().DelegateOne
//target._myDelegate = () => { return 5; };
int original = 7;
int expected = 1;
int actual;
//7%2=1;
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