????Microsoft Fakes ???????????????????????е??????

????????????????????????????????????????????仯??????????μ?仯??????????????????????????

?????????????????????????????????????????????

 ????? ??Stub ?? Shim

????Microsoft Fakes ?????????????????????.???????????????????????????????????????.

????1.Stub Type?????????????????????????????virtual???????????У?????????????????????????????.

????2.Shim Types?????????????????????static?????????T?????????ShimT?????T???????????????????

?????? ????????

????????Stub??Shim??????????????????????в?????????????????????????Щ???:

???????????:????????Shim??д??????????Stub???????????鷽?????????????

??????static????/sealed????:Stub???????????д?鷽??????????????????static????/sealed????/sealed???е????????

????Internal????:????????InternalsVisibleToAttribute??????????Fakes???????????

????private????:???private????????????????????????????????????????Shim???滻???.Stub????滻???????.

?????????????:Stub???????????????????????.Shim????????????????????.

????????????????????????Stub???????Щ?????????????????????????Shim???????Щ??????????????????????????.

??????????????Fakes

?????????????????ClassLibrary1?????????????

????1: public interface IDataAccess

????2: {

????3: int Read();

????4: }

????5:

????6: public class MyDataAccess : IDataAccess

????7: {

????8: public int Read()

????9: {

????10: return Tools.GetNum();

????11: }

????12: }

????13:

????14: public class Tools

????15: {

????16: static public int GetNum()

????17: {

????18: return 1;

????19: }

????20: }

????21:

????22: public class MyClass

????23: {

????24: public static int GetMyData(IDataAccess obj)

????25: {

????26: //???????

????27: return obj.Read();

????28: }

????29: }

????????????Fakes???в????????????????????? ClassLibrary1??????????????->???? Fakes???????Fakes

????????????????????????′???????????IDataAccess????? ??MyClass.GetMyData ???static?????????

???????????Stub???????Shim

????1: [TestClass]

????2: public class UnitTest1

????3: {

????4: [TestMethod]

????5: public void StubTest()

????6: {

????7: IDataAccess stockFeed = new ClassLibrary1.Fakes.StubIDataAccess()

????8: {

????9: Read = () => { return 2; }

????10: };

????11: Assert.AreEqual(2?? MyClass.GetMyData(stockFeed));

????12:

????13: }

????14: [TestMethod]

????15: public void ShimTest()

????16: {

????17: using (ShimsContext.Create())

????18: {

????19: ClassLibrary1.Fakes.ShimMyClass.GetMyDataIDataAccess = (inc) => { return 2; };

????20: Assert.AreEqual(2?? MyClass.GetMyData(null));

????21: }

????22:

????23: }

????24:

????25: }

????Stub ??????Mock???????????????????????

??????Shim????????? ShimsContext????????