Created
March 7, 2012 03:48
-
-
Save hatelove/1990816 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; | |
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