Wrox Programmer Forums
|
BOOK: Professional ASP.NET Design Patterns
This is the forum to discuss the Wrox book Professional ASP.NET Design Patterns by Scott Millett; ISBN: 978-0-470-29278-5
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Professional ASP.NET Design Patterns section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 30th, 2011, 07:26 AM
Registered User
 
Join Date: Jan 2011
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default Extension methods and testing

Scott, You seem to use a lot of extension methods and I agree that it keeps the code clean, but...

Doesn't it make unit testing a lot harder?
 
Old April 9th, 2011, 11:45 AM
elbandit's Avatar
Wrox Author
 
Join Date: May 2007
Posts: 107
Thanks: 10
Thanked 17 Times in 15 Posts
Default

Hi MrFixxiT,

I haven't had any problems when trying to test them. Take a look at this example..

Code:
 
    [TestFixture]
    public class when_determining_the_tax_code_for_an_employee_earning_99_for_the_last_year
    {
        private IPerson person;
        private string result;
        [SetUp]
        public void given()
        {
            person = MockRepository.GenerateStub<IPerson>();
            person.Stub(x => x.earnings_for_the_last_year).Return(99m);
            result = person.calculate_code_rate();
        }
        [Test]
        public void it_should_get_the_tax_code_GHYK9()
        {
            Assert.That(result, Is.EqualTo("GHYK9"));
        }
        [Test]
        public void it_should_ask_the_employee_for_earnings_for_the_last_year()
        {
            person.AssertWasCalled(x => x.earnings_for_the_last_year);
        }
    }
 
    public interface IPerson
    {
        decimal earnings_for_the_last_year { get; }
    }
    public static class PersonExtensions
    {
        public static string calculate_code_rate(this IPerson person)
        {
            if (person.earnings_for_the_last_year > 100)
                return "EK9";
            else            
                return "GHYK9";            
        }
    }
I try and tend to use extension methods sparinly now, mostly use them for infrastructure bits and bobs like the below (which is nice and easy to test) to keep noise out of the code..

Code:
 
    public static class ComparisonExtensions
    {
        public static bool implements_interface<TInterface>(this object object_to_compare)
        {
            return typeof (TInterface).IsAssignableFrom(object_to_compare.GetType());
        }
        public static bool is_type_of<TType>(this object object_to_compare)
        {
            return object_to_compare.GetType() == typeof(TType);
        }
    }
Does that help? Please let me know if you want some more info.

Cheers
Scottt
 
Old April 14th, 2011, 05:37 AM
Registered User
 
Join Date: Jan 2011
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

That helps, thanks.

Extension methods just don't work if you want them to be mocked by a mocking framework (only Typemock Isolator does that I think), but I guess you should not use extension methods for things you might want to mock some day.

The results of your example extension methods are pretty straight forward and directly based on some input. So just test the method itself and mock any preconditions, just like you did.

I could think of something that you don't want to specify the preconditions to have the extension method return something, because you just want to test some code that depends on certain results of your extension method without the need (and knowledge) of how the extension method decides what to return.
But in this situation the extension method is probably too much integrated into the class for it to be an extension method anyways.

Do I make sense and so answered my own reservation against extension methods? Thanks again.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Chapter 7: Unit Testing with the Unit Testing Framework antwarpes BOOK: Professional Application Lifecycle Management with Visual Studio 2010 3 September 25th, 2011 08:47 AM
Extension methods vs inheritance perstam C# 2008 aka C# 3.0 3 March 2nd, 2010 06:35 PM
METHODS magagulad Java GUI 3 May 15th, 2007 12:53 PM
Dispose() methods bpevangelista BOOK: Professional XNA Game Programming: For Xbox 360 and Windows ISBN: 978-0-470-12677-6 2 May 12th, 2007 11:15 PM
Treeview Methods goldwinger C# 2005 0 December 29th, 2005 10:00 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.