Best JustMockLite code snippet using Telerik.JustMock.Setup.FluentConfig
MockFixture.cs
Source:MockFixture.cs
...1923 internal abstract class InternalAbstract1924 {1925 internal abstract string Bar { get; set; }1926 }1927 #region Category "FluentConfig"1928 public class Base1929 {1930 public const int DefaultValue = 3;1931 public int i = DefaultValue;1932 public Base(int i)1933 {1934 this.i = i;1935 }1936 }1937 [TestMethod, TestCategory("Lite"), TestCategory("Mock"), TestCategory("FluentConfig")]1938 public void ShouldUseAutoselectedConstructorMockingBehaviorWithFluentGenericConfig()1939 {1940 var proxy = Mock.Create<Base>(fluentConfig => { });1941 Assert.Equal(default(int), proxy.i);1942 Assert.Null(proxy as IDisposable);1943 }1944 [TestMethod, TestCategory("Lite"), TestCategory("Mock"), TestCategory("FluentConfig")]1945 public void ShouldMockWhenMissingPameterlessConstructorAndRecursiveLooseWithFluentGenericConfig()1946 {1947 var proxy = Mock.Create<Base>(fluentConfig =>1948 fluentConfig.SetBehavior(Behavior.RecursiveLoose)1949 );1950 Assert.Equal(default(int), proxy.i);1951 Assert.Null(proxy as IDisposable);1952 }1953 [TestMethod, TestCategory("Lite"), TestCategory("Mock"), TestCategory("FluentConfig")]1954 public void ShouldMockWhenMissingPameterlessConstructorAndLooseWithFluentGenericConfig()1955 {1956 var proxy = Mock.Create<Base>(fluentConfig =>1957 fluentConfig.SetBehavior(Behavior.Loose)1958 );1959 Assert.Equal(default(int), proxy.i);1960 Assert.Null(proxy as IDisposable);1961 }1962 // Implementation differs for .NETFramework and .NETCore, see DynamicProxyMockFactory.Create method1963#if !NETCORE1964 [TestMethod, TestCategory("Lite"), TestCategory("Mock"), TestCategory("FluentConfig")]1965 public void ShouldThrowWhenMissingPameterlessConstructorAndCallOriginalWithFluentGenericConfig()1966 {1967 Assert.Throws<MockException>(() =>1968 Mock.Create<Base>(fluentConfig =>1969 fluentConfig.SetBehavior(Behavior.CallOriginal))1970 );1971 }1972#else1973 [TestMethod, TestCategory("Lite"), TestCategory("Mock"), TestCategory("FluentConfig")]1974 public void ShouldMockWhenMissingPameterlessConstructorAndCallOriginalWithFluentGenericConfig()1975 {1976 var proxy = Mock.Create<Base>(fluentConfig =>1977 fluentConfig.SetBehavior(Behavior.CallOriginal)1978 );1979 Assert.Equal(default(int), proxy.i);1980 Assert.Null(proxy as IDisposable);1981 }1982#endif1983 [TestMethod, TestCategory("Lite"), TestCategory("Mock"), TestCategory("FluentConfig")]1984 public void ShouldThrowWhenMockConstructorAndCallConstructorWithFluentGenericConfig()1985 {1986 Assert.Throws<MockException>(() =>1987 Mock.Create<Base>(fluentConfig =>1988 fluentConfig.MockConstructor().CallConstructor(new object[] { 5 }))1989 );1990 }1991 [TestMethod, TestCategory("Lite"), TestCategory("Mock"), TestCategory("FluentConfig")]1992 public void ShouldThrowWhenCallConstructorAndMockConstructorWithFluentGenericConfig()1993 {1994 Assert.Throws<MockException>(() =>1995 Mock.Create<Base>(fluentConfig =>1996 fluentConfig.CallConstructor(new object[] { 5 }).MockConstructor())1997 );1998 }1999 [TestMethod, TestCategory("Lite"), TestCategory("Mock"), TestCategory("FluentConfig")]2000 public void ShouldSpecifyConstructorArgumentsWithFluentGenericConfig()2001 {2002 var proxy = Mock.Create<Base>(fluentConfig =>2003 fluentConfig.CallConstructor(new object[] { 5 })2004 );2005 Assert.Equal(5, proxy.i);2006 Assert.Null(proxy as IDisposable);2007 }2008 [TestMethod, TestCategory("Lite"), TestCategory("Mock"), TestCategory("FluentConfig")]2009 public void ShouldMockConstructorWithFluentGenericConfig()2010 {2011 var proxy = Mock.Create<Base>(fluentConfig =>2012 fluentConfig.MockConstructor()2013 );2014 Assert.Equal(default(int), proxy.i);2015 Assert.Null(proxy as IDisposable);2016 }2017 [TestMethod, TestCategory("Lite"), TestCategory("Mock"), TestCategory("FluentConfig")]2018 public void ShouldImplementInterfaceWithFluentGenericConfig()2019 {2020 var proxy = Mock.Create<Base>(fluentConfig =>2021 fluentConfig.Implements<IDisposable>()2022 );2023 Assert.Equal(default(int), proxy.i);2024 Assert.NotNull(proxy as IDisposable);2025 }2026 [TestMethod, TestCategory("Lite"), TestCategory("Mock"), TestCategory("FluentConfig")]2027 public void ShouldUseAutoselectedConstructorMockingBehaviorWithFluentConfig()2028 {2029 var proxy = (Base)Mock.Create(typeof(Base), fluentConfig => { });2030 Assert.Equal(default(int), proxy.i);2031 }2032 [TestMethod, TestCategory("Lite"), TestCategory("Mock"), TestCategory("FluentConfig")]2033 public void ShouldMockWhenMissingPameterlessConstructorAndRecursiveLooseWithFluentConfig()2034 {2035 var proxy = (Base)Mock.Create(typeof(Base), fluentConfig =>2036 fluentConfig.SetBehavior(Behavior.RecursiveLoose)2037 );2038 Assert.Equal(default(int), proxy.i);2039 }2040 [TestMethod, TestCategory("Lite"), TestCategory("Mock"), TestCategory("FluentConfig")]2041 public void ShouldMockWhenMissingPameterlessConstructorAndLooseWithFluentConfig()2042 {2043 var proxy = (Base)Mock.Create(typeof(Base), fluentConfig =>2044 fluentConfig.SetBehavior(Behavior.Loose)2045 );2046 Assert.Equal(default(int), proxy.i);2047 }2048 // Implementation differs for .NETFramework and .NETCore, see DynamicProxyMockFactory.Create method2049#if !NETCORE2050 [TestMethod, TestCategory("Lite"), TestCategory("Mock"), TestCategory("FluentConfig")]2051 public void ShouldThrowWhenMissingPameterlessConstructorAndCallOriginalWithFluentConfig()2052 {2053 Assert.Throws<MockException>(() =>2054 Mock.Create(typeof(Base), fluentConfig =>2055 fluentConfig.SetBehavior(Behavior.CallOriginal))2056 );2057 }2058#else2059 [TestMethod, TestCategory("Lite"), TestCategory("Mock"), TestCategory("FluentConfig")]2060 public void ShouldMockWhenMissingPameterlessConstructorAndCallOriginalWithFluentConfig()2061 {2062 var proxy = (Base)Mock.Create(typeof(Base), fluentConfig =>2063 fluentConfig.SetBehavior(Behavior.CallOriginal)2064 );2065 Assert.Equal(default(int), proxy.i);2066 }2067#endif2068 [TestMethod, TestCategory("Lite"), TestCategory("Mock"), TestCategory("FluentConfig")]2069 public void ShouldThrowWhenMockConstructorAndCallConstructorWithFluentConfig()2070 {2071 Assert.Throws<MockException>(() =>2072 Mock.Create(typeof(Base), fluentConfig =>2073 fluentConfig.MockConstructor().CallConstructor(new object[] { 5 }))2074 );2075 }2076 [TestMethod, TestCategory("Lite"), TestCategory("Mock"), TestCategory("FluentConfig")]2077 public void ShouldThrowWhenCallConstructorAndMockConstructorWithFluentConfig()2078 {2079 Assert.Throws<MockException>(() =>2080 Mock.Create(typeof(Base), fluentConfig =>2081 fluentConfig.CallConstructor(new object[] { 5 }).MockConstructor())2082 );2083 }2084 [TestMethod, TestCategory("Lite"), TestCategory("Mock"), TestCategory("FluentConfig")]2085 public void ShouldSpecifyConstructorArgumentsWithFluentConfig()2086 {2087 var proxy = (Base)Mock.Create(typeof(Base), fluentConfig =>2088 fluentConfig.CallConstructor(new object[] { 5 })2089 );2090 Assert.Equal(5, proxy.i);2091 }2092 [TestMethod, TestCategory("Lite"), TestCategory("Mock"), TestCategory("FluentConfig")]2093 public void ShouldMockConstructorWithFluentConfig()2094 {2095 var proxy = (Base)Mock.Create(typeof(Base), fluentConfig =>2096 fluentConfig.MockConstructor()2097 );2098 Assert.Equal(default(int), proxy.i);2099 }2100 #endregion2101 }2102}...
Mock.Create.cs
Source:Mock.Create.cs
...60 /// </summary>61 /// <typeparam name="T">Type of the mock</typeparam>62 /// <param name="settings">Specifies mock settings</param>63 /// <returns>Mock instance</returns>64 public static T Create<T>(Action<IFluentConfig<T>> settings)65 {66 return ProfilerInterceptor.GuardInternal(() =>67 {68 var fluentConfig = new FluentConfig<T>();69 settings(fluentConfig);70 return (T)fluentConfig.CreateMock(typeof(T), MockingContext.CurrentRepository);71 });72 }73 /// <summary>74 /// Creates a mocked instance from a given type.75 /// </summary>76 /// <typeparam name="T">Type of the mock</typeparam>77 /// <param name="behavior">Specifies behavior of the mock. Default is <see cref="Behavior.RecursiveLoose"/></param>78 /// <param name="args">Constructor arguments</param>79 /// <returns>Mock instance</returns>80 public static T Create<T>(Behavior behavior, params object[] args)81 {82 return ProfilerInterceptor.GuardInternal(() =>83 {84 MockCreationSettings settings = MockCreationSettings.GetSettings(args, behavior, null, null);85 return (T)MockingContext.CurrentRepository.Create(typeof(T), settings);86 });87 }88 /// <summary>89 /// Creates a mocked instance from a given type.90 /// </summary>91 /// <typeparam name="T">Type of the mock</typeparam>92 /// <param name="behavior">Specifies behavior of the mock. Default is <see cref="Behavior.RecursiveLoose"/></param>93 /// <returns>Mock instance</returns>94 public static T Create<T>(Behavior behavior)95 {96 return ProfilerInterceptor.GuardInternal(() =>97 {98 MockCreationSettings settings = MockCreationSettings.GetSettings(behavior);99 return (T)MockingContext.CurrentRepository.Create(typeof(T), settings);100 });101 }102 /// <summary>103 /// Creates a mocked instance from a given type with <see cref="Behavior.RecursiveLoose"/> behavior.104 /// </summary>105 /// <typeparam name="T">Type of the mock</typeparam>106 /// <returns>Mock instance</returns>107 public static T Create<T>()108 {109 return ProfilerInterceptor.GuardInternal(() =>110 {111 MockCreationSettings settings = MockCreationSettings.GetSettings();112 return (T)MockingContext.CurrentRepository.Create(typeof(T), settings);113 });114 }115 /// <summary>116 /// Creates a mocked instance from a given type with <see cref="Behavior.RecursiveLoose"/> behavior.117 /// </summary>118 /// <param name="target">Target to mock</param>119 /// <param name="args">Constructor arguments</param>120 /// <returns>Mock instance</returns>121 public static object Create(Type target, params object[] args)122 {123 return ProfilerInterceptor.GuardInternal(() =>124 {125 MockCreationSettings settings = MockCreationSettings.GetSettings(args, null, null, null);126 return MockingContext.CurrentRepository.Create(target, settings);127 });128 }129 /// <summary>130 /// Creates a mocked instance from a given type with <see cref="Behavior.RecursiveLoose"/> behavior.131 /// </summary>132 /// <param name="target">Target to mock</param>133 /// <returns>Mock instance</returns>134 public static object Create(Type target)135 {136 return ProfilerInterceptor.GuardInternal(() =>137 {138 MockCreationSettings settings = MockCreationSettings.GetSettings();139 return MockingContext.CurrentRepository.Create(target, settings);140 });141 }142 /// <summary>143 /// Creates a mocked instance from a given type with <see cref="Behavior.RecursiveLoose"/> behavior.144 /// </summary>145 /// <param name="constructor">146 /// Specifies whether to call the base constructor147 /// </param>148 /// <typeparam name="T">Target type</typeparam>149 /// <returns>Mock instance</returns>150 public static T Create<T>(Constructor constructor)151 {152 return ProfilerInterceptor.GuardInternal(() =>153 {154 MockCreationSettings settings = MockCreationSettings.GetSettings(null, null, null, constructor == Constructor.Mocked);155 return (T)MockingContext.CurrentRepository.Create(typeof(T), settings);156 });157 }158 /// <summary>159 /// Creates a mocked instance from a given type.160 /// </summary>161 /// <param name="constructor">162 /// Specifies whether to call the base constructor163 /// </param>164 /// <param name="behavior">Specifies behavior of the mock. Default is <see cref="Behavior.RecursiveLoose"/></param>165 /// <returns>Mock instance</returns>166 /// <typeparam name="T">Target type</typeparam>167 public static T Create<T>(Constructor constructor, Behavior behavior)168 {169 return ProfilerInterceptor.GuardInternal(() =>170 {171 MockCreationSettings settings = MockCreationSettings.GetSettings(null, behavior, null, constructor == Constructor.Mocked);172 return (T)MockingContext.CurrentRepository.Create(typeof(T), settings);173 });174 }175 /// <summary>176 /// Creates a mocked instance from a given type.177 /// </summary>178 /// <param name="type">Target to mock</param>179 /// <param name="constructor">180 /// Specifies whether to call the base constructor181 /// </param>182 /// <param name="behavior">Specifies behavior of the mock. Default is <see cref="Behavior.RecursiveLoose"/></param>183 /// <returns>Mock instance</returns>184 public static object Create(Type type, Constructor constructor, Behavior behavior)185 {186 return ProfilerInterceptor.GuardInternal(() =>187 {188 MockCreationSettings settings = MockCreationSettings.GetSettings(null, behavior, null, constructor == Constructor.Mocked);189 return MockingContext.CurrentRepository.Create(type, settings);190 });191 }192 /// <summary>193 /// Creates a mocked instance from a given type.194 /// </summary>195 /// <param name="type">Target type to mock</param>196 /// <param name="settings">Mock settings</param>197 /// <returns>Mock instance</returns>198 public static object Create(Type type, Action<IFluentConfig> settings)199 {200 return ProfilerInterceptor.GuardInternal(() =>201 {202 var fluentConfig = new FluentConfig();203 settings(fluentConfig);204 return fluentConfig.CreateMock(type, MockingContext.CurrentRepository);205 });206 }207 /// <summary>208 /// Creates a mock instance from a given type.209 /// </summary>210 /// <param name="type">Mocking type</param>211 /// <param name="behavior">Specifies behavior of the mock. Default is <see cref="Behavior.RecursiveLoose"/></param>212 /// <param name="args">Constructor arguments</param>213 /// <returns>Mock instance</returns>214 public static object Create(Type type, Behavior behavior, params object[] args)215 {216 return ProfilerInterceptor.GuardInternal(() =>217 {218 MockCreationSettings settings = MockCreationSettings.GetSettings(args, behavior, null, null);219 return MockingContext.CurrentRepository.Create(type, settings);220 });221 }222 /// <summary>223 /// Creates a mock instance from a given type.224 /// </summary>225 /// <param name="type">Mocking type</param>226 /// <param name="behavior">Specifies behavior of the mock. Default is <see cref="Behavior.RecursiveLoose"/></param>227 /// <returns>Mock instance</returns>228 public static object Create(Type type, Behavior behavior)229 {230 return ProfilerInterceptor.GuardInternal(() =>231 {232 MockCreationSettings settings = MockCreationSettings.GetSettings(behavior);233 return MockingContext.CurrentRepository.Create(type, settings);234 });235 }236 /// <summary>237 /// Creates a mocked instance from a given type with <see cref="Behavior.RecursiveLoose"/> behavior.238 /// </summary>239 /// <typeparam name="T">Mocked object type.</typeparam>240 /// <param name="args">Constructor arguments</param>241 /// <returns>Mock instance</returns>242 public static T Create<T>(params object[] args)243 {244 return ProfilerInterceptor.GuardInternal(() =>245 {246 MockCreationSettings settings = MockCreationSettings.GetSettings(args, null, null, null);247 return (T)MockingContext.CurrentRepository.Create(typeof(T), settings);248 });249 }250 /// <summary>251 /// Creates a mock with <see cref="Behavior.RecursiveLoose"/> behavior by parsing the given functional specification.252 /// </summary>253 /// <remarks>254 /// See article "Create Mocks By Example" for further information on how to write functional specifications.255 /// </remarks>256 /// <typeparam name="T">Type for the argument.</typeparam>257 /// <param name="functionalSpecification">The functional specification to apply to the mock object.</param>258 /// <returns>A mock with the given functional specification.</returns>259 public static T CreateLike<T>(Expression<Func<T, bool>> functionalSpecification)260 {261 return ProfilerInterceptor.GuardInternal(() =>262 {263 var mock = Mock.Create<T>();264 ArrangeLike(mock, functionalSpecification);265 return mock;266 });267 }268#if !LITE_EDITION269 /// <summary>270 /// Creates a mocked instance from a internal class with <see cref="Behavior.RecursiveLoose"/> behavior.271 /// </summary>272 /// <param name="fullName">Fully qualified name of the target type.</param>273 /// <returns>Mock instance</returns>274 public static object Create(string fullName)275 {276 return ProfilerInterceptor.GuardInternal(() =>277 {278 MockCreationSettings settings = MockCreationSettings.GetSettings();279 return MockingContext.CurrentRepository.Create(MockingUtil.GetTypeFrom(fullName), settings);280 });281 }282 /// <summary>283 /// Creates a mocked instance from an internal class.284 /// </summary>285 /// <param name="fullName">Fully qualified name of the target type.</param>286 /// <param name="behavior">Specifies behavior of the mock. Default is <see cref="Behavior.RecursiveLoose"/></param>287 /// <returns>Mock instance</returns>288 public static object Create(string fullName, Behavior behavior)289 {290 return ProfilerInterceptor.GuardInternal(() =>291 {292 MockCreationSettings settings = MockCreationSettings.GetSettings(behavior);293 return MockingContext.CurrentRepository.Create(MockingUtil.GetTypeFrom(fullName), settings);294 });295 }296 /// <summary>297 /// Creates a mocked instance from an internal class.298 /// </summary>299 /// <param name="fullName">Fully qualified name of the target type.</param>300 /// <param name="settings">Settings for the mock</param>301 /// <returns>Mock instance</returns>302 public static object Create(string fullName, Action<IFluentConfig> settings)303 {304 return ProfilerInterceptor.GuardInternal(() =>305 {306 var fluentConfig = new FluentConfig();307 settings(fluentConfig);308 return fluentConfig.CreateMock(MockingUtil.GetTypeFrom(fullName), MockingContext.CurrentRepository);309 });310 }311#endif312 }313}...
FluentConfig.cs
Source:FluentConfig.cs
...20using Telerik.JustMock.Abstraction;21using Telerik.JustMock.Core;22namespace Telerik.JustMock.Setup23{24 internal class FluentConfig<T> : FluentConfig, IFluentConfig<T>25 {26 private readonly List<Type> implementedInterfaces = new List<Type>();27 public IFluentConfig<T> Implements<TInterface>()28 {29 implementedInterfaces.Add(typeof(TInterface));30 return this;31 }32 public IFluentConfig<T> CallConstructor(Expression<Func<T>> expression)33 {34 if (mockConstructor == true)35 {36 throw new MockException("The constructor is already configured to be mocked. Remove the previous call to MockConstructor() if you want to call a constructor.");37 }38 this.arguments = expression.GetArgumentsFromConstructorExpression();39 return this;40 }41 public override object CreateMock(Type mockType, MocksRepository repository)42 {43 MockCreationSettings settings = MockCreationSettings.GetSettings(this.arguments, this.behavior, this.implementedInterfaces.ToArray(),44 this.mockConstructor, this.additionalProxyTypeAttributes, null, null, null, this.interceptorFilter);45 return repository.Create(mockType, settings);46 }47 }48 internal class FluentConfig : IFluentConfig49 {50 protected Behavior? behavior;51 protected bool? mockConstructor;52 protected List<CustomAttributeBuilder> additionalProxyTypeAttributes;53 protected Expression<Predicate<MethodInfo>> interceptorFilter;54 protected object[] arguments;55 public IFluentConfig AddAttributeToProxy(CustomAttributeBuilder attributeBuilder)56 {57 if (additionalProxyTypeAttributes == null)58 additionalProxyTypeAttributes = new List<CustomAttributeBuilder>();59 additionalProxyTypeAttributes.Add(attributeBuilder);60 return this;61 }62 public IFluentConfig SetBehavior(Behavior behavior)63 {64 this.behavior = behavior;65 return this;66 }67 public IFluentConfig MockConstructor()68 {69 if (mockConstructor.HasValue && mockConstructor == false)70 {71 throw new MockException("A constructor is already configured to be called. Remove the previous call to CallConstructor() if you want to mock the constructor.");72 }73 this.mockConstructor = true;74 return this;75 }76 public virtual object CreateMock(Type mockType, MocksRepository repository)77 {78 MockCreationSettings settings = MockCreationSettings.GetSettings(this.arguments, this.behavior, null, this.mockConstructor,79 this.additionalProxyTypeAttributes, null, null, null, this.interceptorFilter);80 return repository.Create(mockType, settings);81 }82 public IFluentConfig SetInterceptorFilter(Expression<Predicate<MethodInfo>> filter)83 {84 this.interceptorFilter = filter;85 return this;86 }87 public IFluentConfig CallConstructor(object[] args)88 {89 if (mockConstructor.HasValue && mockConstructor == true)90 {91 throw new MockException("The constructor is already configured to be mocked. Remove the previous call to MockConstructor() if you want to call a constructor.");92 }93 this.mockConstructor = false;94 this.arguments = args;95 return this;96 }97 }98}...
FluentConfig
Using AI Code Generation
1using Telerik.JustMock;2using Telerik.JustMock.Setup;3using System;4using System.Collections.Generic;5using System.Linq;6using System.Text;7using System.Threading.Tasks;8{9 {10 public string Name { get; set; }11 public int Age { get; set; }12 public Class1(string name, int age)13 {14 this.Name = name;15 this.Age = age;16 }17 public Class1()18 {19 }20 public virtual int Add(int a, int b)21 {22 return a + b;23 }24 public virtual void DoSomething()25 {26 Console.WriteLine("Do Something");27 }28 public virtual void DoSomething(int i)29 {30 Console.WriteLine("Do Something");31 }32 public virtual void DoSomething(string s)33 {34 Console.WriteLine("Do Something");35 }36 public virtual void DoSomething(string s, int i)37 {38 Console.WriteLine("Do Something");39 }40 public virtual void DoSomething(int i, string s)41 {42 Console.WriteLine("Do Something");43 }44 }45 {46 public virtual string GetSomething()47 {48 return "Something";49 }50 }51 {52 public virtual Class1 GetSomething()53 {54 return new Class1();55 }56 }57}58using Telerik.JustMock;59using Telerik.JustMock.Setup;60using System;61using System.Collections.Generic;62using System.Linq;63using System.Text;64using System.Threading.Tasks;65{66 {67 public virtual void DoSomething()68 {69 Console.WriteLine("Do Something");70 }71 }72}73using Telerik.JustMock;74using Telerik.JustMock.Setup;75using System;76using System.Collections.Generic;77using System.Linq;78using System.Text;79using System.Threading.Tasks;80{81 {82 public virtual void DoSomething()83 {84 Console.WriteLine("Do Something");85 }86 public virtual void DoSomething(int i)87 {88 Console.WriteLine("Do Something");
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!