Created
March 7, 2012 04:04
-
-
Save hatelove/1990870 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//第三版 | |
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