Protected???????????
???????????? ???????[ 2012/11/30 13:26:23 ] ????????
?????????????protected??????????Mock?????????????????Щ??????????檔?????????????£???????????????????protected??????????????????????o????????????????????????????μ?????????
protected??????????????????η????????????????????????????????????????????????仰???????ù????У?protected?????????????????????????????????????ó??????????????????????????????г???????????????????????????????protected??????????ε?????????“????”??ζ???????????????е?????????“???”???譚?????????????????漰protected?????
????protected????
??????????????а??????protected??????
public class SomeClass
{
protected int SomeMethod(string arg) { ... }
}
???????????????protected???????е????????????????????????????????????
public class SomeClassForTest : SomeClass
{
public int PublicSomeMethod(string arg)
{
return this.SomeMethod(arg);
}
}
?????????????У?????????????PublicSomeMethod??????????SomeMethod??????
var testClass = new SomeClassForTest();
var result = testClass.PublicSomeMethod(null);
Assert.Equal(0?? result);
???????
??????????鷳????????SomeClass???е?SomeMethod???????protected internal?????????????InternalVisibleTo?????????????????????????????????????????????????????????????????
??protected????????Mock
????????????????????protected??????
public class SomeClass
{
protected virtual int SomeMethod(string arg) { ... }
}
??????????????????????SomeClass????????????????????????????????SomeMethod??????????SomeMethod???????????????????????????????????????SomeMethod????Mock??Stub???????????????????????????????
public class MockSomeClass : SomeClass
{
protected override int SomeMethod(string arg)
{
return this.PublicSomeMethod(arg);
}
public virtual int PublicSomeMethod(string arg)
{
return base.SomeMethod(arg);
}
}
??MockSomeClass?У??????????????SomeMethod??????????????????й?????PublicSomeMethod????????PublicSomeMethod??????????????SomeMethod???????????????????????κδ???????MockSomeClass????SomeMethod????????????????????SomeMethod????Mock??Stub???????????PublicSomeMethod?????
Mock<MockSomeClass> mockSomeClass = new Mock<MockSomeClass>() { CallBase = true };
mockSomeClass.Setup(c => c.PublicSomeMethod("123")).Returns(123);
DoSomeTest(mockSomeClass.Object); // use the mock object
????????
?????????
?????????????“???????”??????????е?protected??????????virtual????????????????????н???override??????Mock??????????????????????virtual???????????public??????????????????????????????????????ò?????????仰???????????????????override??protected??????????????????????????????????????????????????????????????????????????????????????ò?……???????????????
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11