I ended up using the following:

using System;
using Castle.MicroKernel.Registration;
using Castle.Windsor;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace GPI.REST.UnitTest
{
    public interface IMyContext
    {
        string subtype { get; set; }
    }

    public class MyContext : IMyContext
    {
        public string subtype { get; set; }
    }

    public interface IMyExporter
    {
        string Export();
    }

    public class MyExporterXML : IMyExporter
    {
        public string Export()
        {
            return "XML Data";
        }
    }

    public class MyExporterJson : IMyExporter
    {
        public string Export()
        {
            return "JSON Data";
        }
    }

    public class MyExporterFactory
    {
        private IMyContext context;
        public MyExporterFactory(IMyContext context)
        {
            this.context = context;
        }

        public Func<IMyExporter> Create()
        {
            return () =>
                {
                    if (context.subtype == "XML")
                        return new MyExporterXML();
                    return new MyExporterJson();
                };

        }
    }

    public class MyService
    {
        private readonly Func<IMyExporter> exporter;
        public MyService(Func<IMyExporter> exporter)
        {
            this.exporter = exporter;
        }
        public string Extractdata()
        {
            return exporter().Export();
        }
    }


    [TestClass]
    public class UnitTest2
    {
        public Func<IMyExporter> CreateExporter()
        {
            Func<IMyExporter> smo = null;
            return smo;
        }

        [TestMethod]
        public void TestMethod1()
        {
            // Arrange
            var container = new WindsorContainer();
 
container.Register(Component.For<IMyContext>().ImplementedBy<MyContext>());
            container.Register(Component.For<MyService>());
            container.Register(Component.For<MyExporterFactory>());
 
container.Register(Component.For<Func<IMyExporter>>().UsingFactoryMethod(kernel
=> kernel.Resolve<MyExporterFactory>().Create()));
            var context = container.Resolve<IMyContext>();
            var service = container.Resolve<MyService>();

            // Act
            context.subtype = "JSON";
            var resultJson = service.Extractdata();

            context.subtype = "XML";
            var resultXml = service.Extractdata();

            // Assert
            Assert.AreEqual(resultJson, "JSON Data");
            Assert.AreEqual(resultXml, "XML Data");
        }
    }
}

-- 
You received this message because you are subscribed to the Google Groups 
"Castle Project Users" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/castle-project-users?hl=en.

Reply via email to