Tuesday, August 17, 2004

At first sight it may seem that Singleton and Unit Testing are not compatible since you cannot ensure the separation between the tests. We could solve the problem by instanciating a new "Singleton" instance for each test and apply test on this "local" instance. The problem is that a well implemented Singleton is sealed and it's constructor are private (see Singleton Pattern here) and thus you cannot create this instance.

using System;

public sealed class Singleton
{
   ...
   private Singleton() {}

   public static Singleton Instance
   {
      get { ...}
   }
}

Reflection to the rescue

An elegant way for bypassing the "creation problem" is to use Reflection to get the private constructor (ConstructorInfo) and then use this constructor info to create a new "Singleton" instance.

using System.Reflection;

[TestFixture]
public class SingletonTest
{
    private Singleton target = null;
    [SetUp]
    public void SetUp()
    {
        ConstructorInfo ci =
            typeof(Singleton).GetConstructor(
                BindingFlags.Instance |
                BindingFlags.NonPublic,
                null,
                Type.EmptyTypes,
                null
                );
         Assert.IsNotNull(ci);
         this.target = (Singleton)ci.Invoke(null);
    }
}

Now we have a new instance of the Singleton for each test and we are happy.

posted on Wednesday, August 18, 2004 12:14:00 AM UTC  #    Comments [18]
Tracked by:
"foligno" (online) [Trackback]
http://www.google.com/search?q=elzoxpaf [Pingback]
http://www.google.com/search?q=hzsknojk [Pingback]
http://eastjava.com/tourism/surabaya/guestbook/templates/4/ultram-online.htm [Pingback]
http://www.twojopis.pl/dump/macro/1/carisoprodol.htm [Pingback]
http://indonesiacommerce.com/support/documentation/2/soma.htm [Pingback]
http://eastjava.com/tourism/surabaya/guestbook/templates/4/levitra-buy.htm [Pingback]
http://rattanland.com/teakprod_pics/misc/3/vicodin-online.htm [Pingback]
http://www.grafo.com.pl/item/misc/4/buymeridia.htm [Pingback]
http://rattanland.com/teakprod_pics/misc/1/buyphentermineonline.htm [Pingback]
http://www.twojopis.pl/dump/macro/1/levitra.htm [Pingback]
http://rattanland.com/teakprod_pics/misc/4/overnight-tramadol.htm [Pingback]
http://rattanland.com/teakprod_pics/misc/2/weight-loss-pill.htm [Pingback]
http://www.grafo.com.pl/item/misc/2/zoloft.htm [Pingback]
http://rattanland.com/teakprod_pics/misc/3/effexorxr2.htm [Pingback]
http://eastjava.com/tourism/surabaya/guestbook/templates/3/carisoprodol-online.h... [Pingback]
http://www.grafo.com.pl/item/misc/4/diet-pills.htm [Pingback]
http://eastjava.com/tourism/surabaya/guestbook/templates/3/hydrocodone.htm [Pingback]
http://www.twojopis.pl/dump/macro/2/soma.htm [Pingback]
http://eastjava.com/tourism/surabaya/guestbook/templates/2/health.htm [Pingback]
http://indonesiacommerce.com/support/documentation/2/cheap-viagra.htm [Pingback]
http://indonesiacommerce.com/support/documentation/4/zoloft.htm [Pingback]
http://www.twojopis.pl/dump/macro/1/hoodia.htm [Pingback]
http://rattanland.com/teakprod_pics/misc/2/alprazolam.htm [Pingback]
http://www.kataloog.info/digits/misc/1/vicodin.htm [Pingback]
http://www.twojopis.pl/dump/macro/2/vicodin.htm [Pingback]
http://rattanland.com/teakprod_pics/misc/2/lexapro.htm [Pingback]
http://indonesiacommerce.com/support/documentation/4/doctor.htm [Pingback]
http://eastjava.com/tourism/surabaya/guestbook/templates/4/lipitor.htm [Pingback]
http://indonesiacommerce.com/support/documentation/3/buy-ultram.htm [Pingback]
http://indonesiacommerce.com/support/documentation/2/cialis-online.htm [Pingback]
http://www.twojopis.pl/dump/macro/2/diet.htm [Pingback]
http://eastjava.com/tourism/surabaya/guestbook/templates/4/zoloft.htm [Pingback]
http://eastjava.com/tourism/surabaya/guestbook/templates/2/propecia.htm [Pingback]
http://rattanland.com/teakprod_pics/misc/1/buy-vicodin.htm [Pingback]
http://www.grafo.com.pl/item/misc/1/propecia.htm [Pingback]
http://www.twojopis.pl/dump/macro/2/order-cialis.htm [Pingback]
http://www.grafo.com.pl/item/misc/3/valium.htm [Pingback]
http://www.grafo.com.pl/item/misc/4/order-tramadol.htm [Pingback]
http://www.grafo.com.pl/item/misc/3/online-pharmacy.htm [Pingback]
http://rattanland.com/teakprod_pics/misc/3/cheap-diazepam.htm [Pingback]
http://indonesiacommerce.com/support/documentation/1/levitra.htm [Pingback]
http://www.grafo.com.pl/item/misc/4/meridia.htm [Pingback]
http://www.grafo.com.pl/item/misc/2/ambien.htm [Pingback]
http://www.twojopis.pl/dump/macro/2/nexium.htm [Pingback]
http://indonesiacommerce.com/support/documentation/1/vicodin.htm [Pingback]
http://www.twojopis.pl/dump/macro/4/buyvalium.htm [Pingback]
http://www.twojopis.pl/dump/macro/3/hydrocodone.htm [Pingback]
http://www.kataloog.info/digits/misc/2/doctor.htm [Pingback]
http://www.twojopis.pl/dump/macro/2/tramadol-online.htm [Pingback]
http://www.kataloog.info/digits/misc/2/hydrocodone.htm [Pingback]
http://tennesseebloggers.com/cgi-bin/db/1/health.htm [Pingback]
http://job-interview-advice.net/careerdoover/wp-content/inc/1/carisoprodol.htm [Pingback]
http://tennesseebloggers.com/cgi-bin/db/3/carisoprodol-online.htm [Pingback]
http://tennesseebloggers.com/cgi-bin/db/1/buyviagraonline.htm [Pingback]
http://jaychantell.com/galleryb/albums/misc/4/hydrocodonevicodin.htm [Pingback]
http://jaychantell.com/galleryb/albums/misc/2/tramadol-cod.htm [Pingback]
http://tennesseebloggers.com/cgi-bin/db/4/diet-pills.htm [Pingback]
http://tennesseebloggers.com/cgi-bin/db/4/buycarisoprodol.htm [Pingback]
http://tennesseebloggers.com/cgi-bin/db/4/buy-ultram.htm [Pingback]
http://job-interview-advice.net/careerdoover/wp-content/inc/1/levitra.htm [Pingback]
http://tennesseebloggers.com/cgi-bin/db/4/valium.htm [Pingback]
http://tennesseebloggers.com/cgi-bin/db/4/buy-valium.htm [Pingback]
http://tennesseebloggers.com/cgi-bin/db/1/buy-cialis.htm [Pingback]
http://job-interview-advice.net/careerdoover/wp-content/inc/3/weight-loss.htm [Pingback]
http://jaychantell.com/galleryb/albums/misc/2/diet-pills-online.htm [Pingback]
http://job-interview-advice.net/careerdoover/wp-content/inc/4/diet-pills.htm [Pingback]
http://tennesseebloggers.com/cgi-bin/db/3/hydrocodone.htm [Pingback]
http://tennesseebloggers.com/cgi-bin/db/2/fioricet.htm [Pingback]
http://jaychantell.com/galleryb/albums/misc/2/cvs-pharmacy.htm [Pingback]
http://jaychantell.com/galleryb/albums/misc/3/hoodia-diet-pills.htm [Pingback]
http://tennesseebloggers.com/cgi-bin/db/4/buyphentermine.htm [Pingback]
http://job-interview-advice.net/careerdoover/wp-content/inc/2/genericviagra.htm [Pingback]
http://job-interview-advice.net/careerdoover/wp-content/inc/3/hydrocodone.htm [Pingback]
http://www.slavophilia.com/cgi-bin/yabb/Cache/4/hydrocodone.htm [Pingback]
http://www.molodiez.org/archive/images/old/2/genericviagra.htm [Pingback]
http://www.molodiez.org/archive/images/old/3/order-viagra.htm [Pingback]
http://www.slavophilia.com/cgi-bin/yabb/Cache/3/order-cialis.htm [Pingback]
http://www.unavuelta.com/Fotos/Culo/1/vicodin.htm [Pingback]
http://www.slavophilia.com/cgi-bin/yabb/Cache/1/health2.htm [Pingback]
http://www.slavophilia.com/cgi-bin/yabb/Cache/2/carisoprodol.htm [Pingback]
http://www.slavophilia.com/cgi-bin/yabb/Cache/2/buy-soma.htm [Pingback]
http://www.unavuelta.com/Fotos/Culo/4/order-viagra.htm [Pingback]
http://www.slavophilia.com/cgi-bin/yabb/Cache/3/cheaptramadol.htm [Pingback]
http://www.slavophilia.com/cgi-bin/yabb/Cache/2/nexium.htm [Pingback]
http://www.molodiez.org/archive/images/old/1/tramadolcod.htm [Pingback]
http://www.unavuelta.com/Fotos/Culo/3/zyrtec.htm [Pingback]
http://www.molodiez.org/archive/images/old/4/buytramadolonline.htm [Pingback]
http://www.slavophilia.com/cgi-bin/yabb/Cache/4/viagraonline.htm [Pingback]
http://www.slavophilia.com/cgi-bin/yabb/Cache/3/buy-viagra-online.htm [Pingback]
http://www.slavophilia.com/cgi-bin/yabb/Cache/1/dietpillsonline.htm [Pingback]
http://www.molodiez.org/archive/images/old/1/vicodin.htm [Pingback]
http://www.slavophilia.com/cgi-bin/yabb/Cache/3/ambien.htm [Pingback]
http://www.unavuelta.com/Fotos/Culo/2/buy-xanax-on-line.htm [Pingback]
http://www.molodiez.org/archive/images/old/4/propecia.htm [Pingback]
http://www.molodiez.org/archive/images/old/3/cheapviagra.htm [Pingback]
http://www.unavuelta.com/Fotos/Culo/4/buy-viagra-online.htm [Pingback]
http://www.classical-composers.org/captcha/tempimage/2/weightloss.htm [Pingback]
http://absoblogginlutely.net/gallery/include/misc/2/buyphentermine2.htm [Pingback]
http://absoblogginlutely.net/gallery/include/misc/3/buy-viagra-online.htm [Pingback]
http://absoblogginlutely.net/gallery/include/misc/1/cheap-viagra.htm [Pingback]
http://www.classical-composers.org/captcha/tempimage/1/tramadolonline.htm [Pingback]
http://absoblogginlutely.net/gallery/include/misc/3/carisoprodol.htm [Pingback]
http://wiredinitiative.com/search/searchdata/db/2/order-cialis.htm [Pingback]
http://www.classical-composers.org/captcha/tempimage/1/buy-cialis.htm [Pingback]
http://absoblogginlutely.net/gallery/include/misc/2/ordercialis.htm [Pingback]
http://www.classical-composers.org/captcha/tempimage/4/healthandbeauty.htm [Pingback]
http://www.classical-composers.org/captcha/tempimage/3/nexium.htm [Pingback]
http://www.classical-composers.org/captcha/tempimage/1/genericviagra.htm [Pingback]
http://wiredinitiative.com/search/searchdata/db/1/tramadol-online.htm [Pingback]
http://www.classical-composers.org/captcha/tempimage/1/health.htm [Pingback]
http://wiredinitiative.com/search/searchdata/db/2/buy-phentermine.htm [Pingback]
http://absoblogginlutely.net/gallery/include/misc/3/weightloss.htm [Pingback]
http://absoblogginlutely.net/gallery/include/misc/3/hoodia.htm [Pingback]
http://wiredinitiative.com/search/searchdata/db/4/fitnessequipment.htm [Pingback]
http://ils3.com/links/inc/2/cheap-cialis.htm [Pingback]
http://wiredinitiative.com/search/searchdata/db/2/buytramadolonline.htm [Pingback]
http://wiredinitiative.com/search/searchdata/db/4/healthandbeauty.htm [Pingback]
http://www.dustcollectorexperts.com/resources/img/2/fitnessequipment.htm [Pingback]
http://www.classical-composers.org/captcha/tempimage/2/order-cialis.htm [Pingback]
http://wiredinitiative.com/search/searchdata/db/3/cheaptramadol.htm [Pingback]
http://www.classical-composers.org/captcha/tempimage/1/vicodin.htm [Pingback]
http://absoblogginlutely.net/gallery/include/misc/1/fioricet.htm [Pingback]
http://www.dustcollectorexperts.com/resources/img/2/xanax.htm [Pingback]
http://www.classical-composers.org/captcha/tempimage/2/levitra.htm [Pingback]
http://www.classical-composers.org/captcha/tempimage/2/soma.htm [Pingback]
http://absoblogginlutely.net/gallery/include/misc/4/buy-valium.htm [Pingback]
http://ils3.com/links/inc/3/prescriptiontramadol.htm [Pingback]
http://wiredinitiative.com/search/searchdata/db/1/ultram.htm [Pingback]
http://absoblogginlutely.net/gallery/include/misc/3/generic-cialis.htm [Pingback]
http://ils3.com/links/inc/1/weight-loss-pill.htm [Pingback]
http://ils3.com/links/inc/3/zone-diet.htm [Pingback]
http://wiredinitiative.com/search/searchdata/db/1/vicodin.htm [Pingback]
http://ils3.com/links/inc/1/hydrocodoneonline.htm [Pingback]
http://absoblogginlutely.net/gallery/include/misc/4/propecia.htm [Pingback]
http://www.dustcollectorexperts.com/resources/img/4/diet-pill-phentermine.htm [Pingback]
http://ils3.com/links/inc/4/viagra-order-online.htm [Pingback]
http://ils3.com/links/inc/1/alprazolam.htm [Pingback]
http://wiredinitiative.com/search/searchdata/db/1/generic-viagra.htm [Pingback]
http://wiredinitiative.com/search/searchdata/db/4/doctor.htm [Pingback]
http://bannerbanner.us/bp/2/viagradiscount.htm [Pingback]
http://palmtreecomputersystems.com/livechat/misc/1/drug.htm [Pingback]
http://m5studios.com/callcenter/misc/3/effexor.htm [Pingback]
http://bannerbanner.us/bp/2/ultram-online.htm [Pingback]
http://bannerbanner.us/bp/2/zyrtec.htm [Pingback]
http://palmtreecomputersystems.com/livechat/misc/2/soma-cheap.htm [Pingback]
http://emptystreets.net/anxiety/cache/inc/1/health.htm [Pingback]
http://bannerbanner.us/bp/4/canadian-pharmacy.htm [Pingback]
http://palmtreecomputersystems.com/livechat/misc/4/phentermine-adipex.htm [Pingback]
http://inthefieldonline.net/aroundme/asset/misc/1/buycialis.htm [Pingback]
http://palmtreecomputersystems.com/livechat/misc/2/tramadol-ultram.htm [Pingback]
http://m5studios.com/callcenter/misc/2/dietpillphentermine.htm [Pingback]
http://palmtreecomputersystems.com/livechat/misc/4/viagraorderonline.htm [Pingback]
http://crtransit.org/zORIGsite/uploads/3/ordertramadol.htm [Pingback]
http://palmtreecomputersystems.com/livechat/misc/2/phentermineprescription.htm [Pingback]
http://crtransit.org/zORIGsite/uploads/1/generic-viagra.htm [Pingback]
http://palmtreecomputersystems.com/livechat/misc/1/celebrex.htm [Pingback]
http://bannerbanner.us/bp/1/celebrex.htm [Pingback]
http://inthefieldonline.net/aroundme/asset/misc/1/tramadol-online.htm [Pingback]
http://inthefieldonline.net/aroundme/asset/misc/3/nexium.htm [Pingback]
http://inthefieldonline.net/aroundme/asset/misc/2/weight-loss.htm [Pingback]
http://bannerbanner.us/bp/3/levitra-buy.htm [Pingback]
http://inthefieldonline.net/aroundme/asset/misc/3/order-tramadol.htm [Pingback]
http://m5studios.com/callcenter/misc/4/buy-propecia-online.htm [Pingback]
http://palmtreecomputersystems.com/livechat/misc/3/effexor.htm [Pingback]
http://palmtreecomputersystems.com/livechat/misc/1/xenical.htm [Pingback]
http://buymasterpiece.com/directory/arts/paintings/4/lexapro.htm [Pingback]
http://inthefieldonline.net/aroundme/asset/misc/4/valium.htm [Pingback]
http://crtransit.org/zORIGsite/uploads/4/weight-loss-pill.htm [Pingback]
http://palmtreecomputersystems.com/livechat/misc/2/ephedra-diet-pill.htm [Pingback]
http://palmtreecomputersystems.com/livechat/misc/1/buy-xanax-online.htm [Pingback]
http://inthefieldonline.net/aroundme/asset/misc/3/pharmacy.htm [Pingback]
http://inthefieldonline.net/aroundme/asset/misc/2/xanax.htm [Pingback]
http://buymasterpiece.com/directory/arts/paintings/3/soma-online.htm [Pingback]
http://crtransit.org/zORIGsite/uploads/4/valium.htm [Pingback]
http://emptystreets.net/anxiety/cache/inc/1/levitra.htm [Pingback]
http://bannerbanner.us/bp/4/vicodin-online.htm [Pingback]
http://bannerbanner.us/bp/1/buypropecia.htm [Pingback]
http://crtransit.org/zORIGsite/uploads/3/weight-watchers.htm [Pingback]
http://m5studios.com/callcenter/misc/3/hoodiadietpills.htm [Pingback]
http://m5studios.com/callcenter/misc/3/levitra-buy.htm [Pingback]
http://emptystreets.net/anxiety/cache/inc/2/buy-phentermine.htm [Pingback]
http://buymasterpiece.com/directory/arts/paintings/1/adipex.htm [Pingback]
http://emptystreets.net/anxiety/cache/inc/2/diet-pills.htm [Pingback]
http://crtransit.org/zORIGsite/uploads/2/buycialisonline.htm [Pingback]
http://inthefieldonline.net/aroundme/asset/misc/1/diet.htm [Pingback]
http://palmtreecomputersystems.com/livechat/misc/2/diet-pill-phentermine.htm [Pingback]
http://inthefieldonline.net/aroundme/asset/misc/4/ordercialis.htm [Pingback]
http://buymasterpiece.com/directory/arts/paintings/3/generic-cialis.htm [Pingback]
http://inthefieldonline.net/aroundme/asset/misc/1/cheap-viagra.htm [Pingback]
http://emptystreets.net/anxiety/cache/inc/4/doctor.htm [Pingback]
http://emptystreets.net/anxiety/cache/inc/1/tramadolonline.htm [Pingback]
http://buymasterpiece.com/directory/arts/paintings/2/paxil.htm [Pingback]
http://www.google.com/search?q=nrboevnw [Pingback]
http://www.goal-uk.org/fundraising/funrun/phpform/forms/db/1/levitra.htm [Pingback]
http://www.generator.ie/userfiles/tmp/1/xanax.htm [Pingback]
http://runtimeware.com/forum/images/avatars/common/2/buy-propecia.htm [Pingback]
http://www.goal-uk.org/fundraising/funrun/phpform/forms/db/1/xanax.htm [Pingback]
http://zoeradio.com/archives/2004/3/adipex.htm [Pingback]
http://www.goal-uk.org/fundraising/funrun/phpform/forms/db/2/buy-viagra-online.h... [Pingback]
http://particles.org/forum/misc/3/online-order-phentermine.htm [Pingback]
http://www.goal-uk.org/fundraising/funrun/phpform/forms/db/3/viagra-online.htm [Pingback]
http://zoeradio.com/archives/2004/3/ordertramadol.htm [Pingback]
http://www.aircoach.ie/news/img/4/buy-cialis.htm [Pingback]
http://jackandjill.ie/meeting/avatars/inc/1/levitra.htm [Pingback]
http://ronhollanddesign.com/css/inc/3/buy-valium.htm [Pingback]
http://www.generator.ie/userfiles/tmp/3/buysoma.htm [Pingback]
http://saifulislam.com/v2/wp-admin/inc/1/drug.htm [Pingback]
http://aleembawany.com/wp-content/inc/1/cheap-cialis.htm [Pingback]
http://becauseclothing.com/shop/images/misc/2/propecia.htm [Pingback]
http://www.aircoach.ie/news/img/2/buy-tramadol.htm [Pingback]
http://expressairlinetickets.com/pages/backup/misc/4/buy-phentermine.htm [Pingback]
http://www.focuspocus.org/test/inc/4/weightloss.htm [Pingback]
http://zoeradio.com/archives/2004/4/valium.htm [Pingback]
http://www.goal-uk.org/fundraising/funrun/phpform/forms/db/3/generic-viagra.htm [Pingback]
http://jackandjill.ie/meeting/avatars/inc/2/xanax.htm [Pingback]
http://ronhollanddesign.com/css/inc/1/diet.htm [Pingback]
http://thescrapbookstore.ie/templates/pic/2/buy-viagra-online.htm [Pingback]
http://saifulislam.com/v2/wp-admin/inc/2/buypropecia.htm [Pingback]
http://becauseclothing.com/shop/images/misc/4/valium.htm [Pingback]
http://jackandjill.ie/meeting/avatars/inc/2/buy-cialis.htm [Pingback]
http://aleembawany.com/wp-content/inc/4/vitamin.htm [Pingback]
http://runtimeware.com/forum/images/avatars/common/3/diet-pill-phentermine.htm [Pingback]
http://particles.org/forum/misc/2/ultram-online.htm [Pingback]
http://aleembawany.com/wp-content/inc/1/drug.htm [Pingback]
http://saifulislam.com/v2/wp-admin/inc/4/viagra-discount.htm [Pingback]
http://aleembawany.com/wp-content/inc/3/hydrocodone-online.htm [Pingback]
http://saifulislam.com/v2/wp-admin/inc/3/hydrocodoneonline.htm [Pingback]
http://programmazioneneurolinguistica.com/feeds/4/best-diet-pills.htm [Pingback]
http://arkarpa.org/joomla/images/misc/3/adipex.htm [Pingback]
http://powerlance.com/template/misc/1/buy-online-soma.htm [Pingback]
http://thelosthub.com/templates_c/3/vitamin.htm [Pingback]
http://buildguide.net/forum/templates/subRed/2/diet.htm [Pingback]
http://pickeringpublishing.com/Bringitonhome/archives/4/pharmacy.htm [Pingback]
http://urdustan.com/catalog/images/sys/3/adipex.htm [Pingback]
http://powerlance.com/template/misc/3/cvs-pharmacy.htm [Pingback]
http://powerlance.com/template/misc/1/buy-xanax-online.htm [Pingback]
http://pickeringpublishing.com/Bringitonhome/archives/1/levitra.htm [Pingback]
http://chatcake.com/tmp/1/drug.htm [Pingback]
http://e-rat.org/aaarg/images/misc/3/xanax.htm [Pingback]
http://pickeringpublishing.com/Bringitonhome/archives/3/diet-pills.htm [Pingback]
http://bluehoney.org/bluehoney/images/base/3/zyrtec.htm [Pingback]
http://buildguide.net/forum/templates/subRed/1/orderviagra.htm [Pingback]
http://urdustan.com/catalog/images/sys/4/hydrocodone.htm [Pingback]
http://buildguide.net/forum/templates/subRed/4/viagraonline.htm [Pingback]
http://e-rat.org/aaarg/images/misc/1/levitra.htm [Pingback]
http://powerlance.com/template/misc/1/cheap-cialis.htm [Pingback]
http://powerlance.com/template/misc/4/meridiaonline.htm [Pingback]
http://programmazioneneurolinguistica.com/feeds/4/hoodia-diet-pills.htm [Pingback]
http://buildguide.net/forum/templates/subRed/1/buycialis.htm [Pingback]
http://pickeringpublishing.com/Bringitonhome/archives/1/safeviagrawoman.htm [Pingback]
http://buildguide.net/forum/templates/subRed/1/xanax.htm [Pingback]
http://pickeringpublishing.com/Bringitonhome/archives/2/cheap-viagra.htm [Pingback]
http://bluehoney.org/bluehoney/images/base/1/cheapcialis.htm [Pingback]
http://bluehoney.org/bluehoney/images/base/3/phentermine-prescription.htm [Pingback]
http://arkarpa.org/joomla/images/misc/4/valium.htm [Pingback]
http://programmazioneneurolinguistica.com/feeds/4/canadian-pharmacy.htm [Pingback]
http://bluehoney.org/bluehoney/images/base/4/tramadolultram.htm [Pingback]
http://arkarpa.org/joomla/images/misc/4/hydrocodone.htm [Pingback]
http://urdustan.com/catalog/images/sys/1/diet.htm [Pingback]
http://kitaabghar.com/dir/javascript/4/ephedra-diet-pill.htm [Pingback]
http://kitaabghar.com/dir/javascript/3/phentermine-adipex.htm [Pingback]
http://getindyknow.com/components/com_mymenu/1/weight-loss-program.htm [Pingback]
http://localboard.on.ca/GuestBook/public/d/diet-plan.htm [Pingback]
http://www.eufos-vienna2007.org/fileadmin/template/css/a/online-order-phentermin... [Pingback]
http://www.google.com/search?q=xzzqqqtd [Pingback]
http://shining.com/store/ph/b/buy-soma.htm [Pingback]
http://inlay.com/phpBB/cache/misc/a/soma-cheap.htm [Pingback]
http://getindyknow.com/components/com_mymenu/1/prescription-drug.htm [Pingback]
http://alsysinc.com/images/Image/a/cheap-meridia.htm [Pingback]
http://shop.trovata.com/images/misc/1/zyrtec.htm [Pingback]
http://helpthemknow.com/phplist/attachments/b/zonediet.htm [Pingback]
http://getindyknow.com/components/com_mymenu/2/buy-online-soma.htm [Pingback]
http://inlay.com/phpBB/cache/misc/c/tramadol-discount.htm [Pingback]
http://localboard.on.ca/GuestBook/public/b/buy-online-soma.htm [Pingback]
http://getindyknow.com/components/com_mymenu/2/meridia-online.htm [Pingback]
http://www.alpenhof.it/fileadmin/inc/d/best-prices-on-phentermine.htm [Pingback]
http://helpthemknow.com/phplist/attachments/b/cabbage-soup-diet.htm [Pingback]
http://www.google.com/search?q=xpidcnrq [Pingback]
http://www.google.com/search?q=dtplhvjq [Pingback]
http://localboard.on.ca/GuestBook/public/b/diet-pill-phentermine.htm [Pingback]
http://shop.trovata.com/images/misc/4/weight-loss-supplement.htm [Pingback]
http://www.alpenhof.it/fileadmin/inc/b/howtopassadrugtest.htm [Pingback]
http://shining.com/store/ph/c/valium.htm [Pingback]
http://inlay.com/phpBB/cache/misc/d/hydrocodone-online.htm [Pingback]
http://inlay.com/phpBB/cache/misc/d/diet-plan.htm [Pingback]
http://shop.trovata.com/images/misc/4/orderhydrocodone.htm [Pingback]
"american pie nadia" (online) [Trackback]
"nappy roots awnaw" (online) [Trackback]
Monday, June 06, 2005 5:40:37 PM UTC
This assumes of course that the singleton doesn't deny reflection permission on the private constructor to stop the singleton from becoming a non-singleton. ;) But of course, you can give your unit test permission by signing it or allowing your user to do that.
nospamplease75@yahoo.com (Haacked)
Monday, June 06, 2005 5:40:37 PM UTC
There's quite a problem with your approach.
<br>You assume that all of the initialization is in the constructor and thus limit the programmer of the singleton.
<br>I would have preferred using reflection to reset the value of the private field holding the single object to null on every TearDown then calling GC.Collect().
<br>This would help collect the single object and would cause the singleton to re-create the object when accessed next.
Omer van Kloeten
Monday, June 06, 2005 5:40:38 PM UTC
Haacked,
<br>
<br>In that case the only solution I see is to create a separate AppDomain for each Test where the assembly is loaded/unloaded each time. This could become computationaly intensive...
<br>
<br>Hi Omer,
<br>
<br>I assume that my singleton does not have any static member besides the singleton itself. Therefore I don't really see the problem you are telling me. Did I miss something ?
Jonathan de Halleux
Monday, June 06, 2005 5:40:38 PM UTC
Peli,
<br>
<br>Let's take your code for example and add something neccesary:
<br>
<br>using System;
<br>
<br>public sealed class Singleton
<br>{
<br>. . // I added this, I'm sure you agree.
<br>. . private static Singleton m_Instance = null;
<br>
<br>. . private Singleton() {}
<br>
<br>. . public static Singleton Instance
<br>. . {
<br>. . . . get { ...}
<br>. . }
<br>}
<br>
<br>If you just set, using reflection, the value of m_Instance to null, it would be much easier, since some of the initialization might be in the get accessor for Instance.
<br>Once setting the value of m_Instance to null, you allow GC to collect it and finalize it (you may need to help it out a bit ;).
<br>Using your method might leave the object somewhat not fully initialized and/or not fully disposed.
Omer van Kloeten
Monday, June 06, 2005 5:40:39 PM UTC
Omer,
<br>
<br>this is the true face of the singleton:
<br>
<br>public class Singleton
<br>{
<br>..private static volatile Singleton instance = null;
<br>..private static readonly object syncRoot = new object();
<br>
<br>..public static Singleton Instance
<br>..{
<br>....get
<br>....{
<br>......if (instance == null)
<br>......{
<br>........lock (syncRoot)
<br>........{
<br>..........if (instance == null)
<br>..........{
<br>............instance = new Singleton();
<br>..........}
<br>........}
<br>......}
<br>......return instance;
<br>....}
<br>..}
<br>
<br>}
<br>
<br>In my unit tests, I never call the Instance method, hence the instance field remains null. However, as a measure of security (who knows what the code might be doing) it would be safer to nullify it.
<br>
<br>ps: Private field should be in camel case :)
Jonathan de Halleux
Monday, June 06, 2005 5:40:39 PM UTC
Unit testing a singleton
Darrell Norton's Blog
Monday, June 06, 2005 5:40:39 PM UTC
Why do you never call the Instance property? How else will you get the singleton?
Omer van Kloeten
Monday, June 06, 2005 5:40:40 PM UTC
I entire idea of this is to use test the functionalities of the singleton as if it was a regular (non-singleton) class.
<br>
<br>Therefore, I don't want to use the Instance method because I want to have a clean instance at each test.
Jonathan de Halleux
Monday, June 06, 2005 5:40:40 PM UTC
I entire/The entire
Jonathan de Halleux
Monday, June 06, 2005 5:40:41 PM UTC
Omer van Kloeten's .NET Zen
Monday, June 06, 2005 5:40:41 PM UTC
Omer van Kloeten's .NET Zen
Monday, June 06, 2005 5:40:41 PM UTC
There is a cleaner way. It's a long post, so I posted it on my blog
Darren Oakey
Monday, June 06, 2005 5:40:42 PM UTC
When you need to jump through hoops to write tests you've done it wrong. &lt;a href=&quot;http://blog.dotnetwiki.org/&quot;&gt;Jonathan de Halleux&lt;/a&gt; writes about &lt;a href=&quot;http://blog.dotnetwiki.org/archive/2004/08/17/771.aspx&quot;&gt;testing singletons&lt;/a&gt; and how he can subvert the standard &lt;a href=&quot;http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnpatterns/html/ImpSingletonInCsharp.asp&quot;&gt;C# singleton pattern&lt;/a&gt; to create a new singleton for each test he needs to run for the singleton. &lt;a href=&quot;http://weblogs.asp.net/okloeten/&quot;&gt;Omer&lt;/a&gt; then subverts the rules &lt;a href=&quot;http://weblogs.asp.net/okloeten/archive/2004/08/19/217182.aspx&quot;&gt;another way&lt;/a&gt;. Both are wrong.
/* Rambling comments... */
Monday, June 06, 2005 5:40:42 PM UTC
Comments added on your article...
Jonathan de Halleux
Monday, June 06, 2005 5:40:42 PM UTC
Peli's Blog
Monday, June 06, 2005 5:40:43 PM UTC
Peli's Blog
Monday, June 06, 2005 5:40:43 PM UTC
C#deSamurai
Monday, June 06, 2005 5:40:44 PM UTC
There is a really easy way to mock singltons using TypeMock.NET, see <a target="_new" href="http://www.typemock.com">http://www.typemock.com</a>
<br>you simply set the required expected return values for the singletons methods, And use the singlton as normal. This will also make the test code cleaner as you will be using the code exactly as a client would
Tim
Comments are closed.