Exemple #1
0
        /// <summary>
        /// Gets the Behaviour override of Mock.Of generic method
        /// </summary>
        /// <param name="type">Type to mock</param>
        /// <param name="behavior">Mock's behavior</param>
        /// <returns>Method to get Mock</returns>
        /// <exception cref="Exception"></exception>
        private static Moq.Mock GetMock(this Type type, Behavior behavior = Behavior.Loose)
        {
            var mockOfMethods  = typeof(Mock).GetMethods().Where(p => p.Name == "Of");
            var mockOfSelected = mockOfMethods.Where(p => p.GetParameters().Count() == 1 &&
                                                     p.GetParameters().First().ParameterType == typeof(Moq.MockBehavior)).First();
            var mockOfMethod = mockOfSelected.MakeGenericMethod(type); //Mock.Of<>

            var mockedObject = mockOfMethod.Invoke(null, new object[] { behavior.Convert() });

            var mockGetMethods  = typeof(Mock).GetMethods().Where(p => p.Name == "Get");
            var mockGetSelected = mockGetMethods.Where(p => p.GetParameters().Count() == 1).First();
            var mockGetMethod   = mockGetSelected.MakeGenericMethod(type); //Mock.Get<>

            return((Moq.Mock)mockGetMethod.Invoke(null, new object[] { mockedObject }));
        }