How do I use Moq to mock an extension method?
Extension methods are just static methods in disguise. Mocking frameworks like Moq or Rhinomocks can only create mock instances of objects, this means mocking static methods is not possible.
Extension methods are just static methods in disguise. Mocking frameworks like Moq or Rhinomocks can only create mock instances of objects, this means mocking static methods is not possible.
I have used a Wrapper to get around this problem. Create a wrapper object and pass your mocked method. See Mocking Static Methods for Unit Testing by Paul Irwin, it has nice examples.
Extension methods allow developers to add new methods to the public contract of an existing CLR type, without having to sub-class it or recompile the original type. Extension Methods help blend the flexibility of “duck typing” support popular within dynamic languages today with the performance and compile-time validation of strongly-typed languages. Reference: http://weblogs.asp.net/scottgu/archive/2007/03/13/new-orcas-language-feature-extension-methods.aspx Here is … Read more
There is already a foreach statement included in the language that does the job most of the time. I’d hate to see the following: list.ForEach( item => { item.DoSomething(); } ); Instead of: foreach(Item item in list) { item.DoSomething(); } The latter is clearer and easier to read in most situations, although maybe a bit … Read more
IEnumerable<Customer> filteredList = originalList .GroupBy(customer => customer.CustomerId) .Select(group => group.First());
No. Extension methods require an instance variable (value) for an object. You can however, write a static wrapper around the ConfigurationManager interface. If you implement the wrapper, you don’t need an extension method since you can just add the method directly. public static class ConfigurationManagerWrapper { public static ConfigurationSection GetSection( string name ) { return … Read more
If you are starting with something that has a .Length or .Count (such as ICollection<T>, IList<T>, List<T>, etc) – then this will be the fastest option, since it doesn’t need to go through the GetEnumerator()/MoveNext()/Dispose() sequence required by Any() to check for a non-empty IEnumerable<T> sequence. For just IEnumerable<T>, then Any() will generally be quicker, … Read more
public static bool In<T>(this T source, params T[] list) { if(null==source) throw new ArgumentNullException(“source”); return list.Contains(source); } Allows me to replace: if(reallyLongIntegerVariableName == 1 || reallyLongIntegerVariableName == 6 || reallyLongIntegerVariableName == 9 || reallyLongIntegerVariableName == 11) { // do something…. } and if(reallyLongStringVariableName == “string1” || reallyLongStringVariableName == “string2” || reallyLongStringVariableName == “string3”) { // … Read more