locked
Assistance on Shim for Code RRS feed

  • Question

  • Public System.Data.EntityState RetrieveState<TEntity>(TEntity entity) where TEntity : class { return System.Data.EntityState.Added; }

    Hello all,

    Can somebody help me on how to write Stub/Shim for the above method?

    Thanks & Regards

    Samba

    Friday, February 8, 2013 12:30 PM

Answers

  • Let's say your product code has the following

        public class Class1   {
            public EntityState RetrieveState<TEntity>(TEntity entity) where TEntity : class
            {
                return System.Data.EntityState.Added;
            }
        }   

    In your test, you can shim as.

        public class MyClass { }

        [TestClass]
        public class UnitTest1    {
            [TestMethod]
            public void TestMethod1()        {
                using (ShimsContext.Create())
                {
                    var shim = new ShimClass1();
                    shim.RetrieveStateOf1M0<MyClass>((T) =>
                        {
                            return System.Data.EntityState.Deleted;
                        }
                    );

                    var aClass = new MyClass();
                    Assert.AreEqual(System.Data.EntityState.Deleted, shim.Instance.RetrieveState<MyClass>(aClass));
                }
            }
        }

    You can find Fakes usage from http://msdn.microsoft.com/en-us/library/hh549175.aspx

    HTH

    Patrick Tseng

    Visual Studio ALM team.


    Patrick Tseng MSFT - Developer - Visual Studio Team Architect

    • Marked as answer by Amanda Zhu Monday, February 18, 2013 5:32 AM
    Friday, February 8, 2013 6:26 PM

All replies

  • Let's say your product code has the following

        public class Class1   {
            public EntityState RetrieveState<TEntity>(TEntity entity) where TEntity : class
            {
                return System.Data.EntityState.Added;
            }
        }   

    In your test, you can shim as.

        public class MyClass { }

        [TestClass]
        public class UnitTest1    {
            [TestMethod]
            public void TestMethod1()        {
                using (ShimsContext.Create())
                {
                    var shim = new ShimClass1();
                    shim.RetrieveStateOf1M0<MyClass>((T) =>
                        {
                            return System.Data.EntityState.Deleted;
                        }
                    );

                    var aClass = new MyClass();
                    Assert.AreEqual(System.Data.EntityState.Deleted, shim.Instance.RetrieveState<MyClass>(aClass));
                }
            }
        }

    You can find Fakes usage from http://msdn.microsoft.com/en-us/library/hh549175.aspx

    HTH

    Patrick Tseng

    Visual Studio ALM team.


    Patrick Tseng MSFT - Developer - Visual Studio Team Architect

    • Marked as answer by Amanda Zhu Monday, February 18, 2013 5:32 AM
    Friday, February 8, 2013 6:26 PM
  • Thank you Patrick,

    I will implement and let you know the feedback.

    Thanks & Regards

    Samba

    Saturday, February 9, 2013 1:41 PM