This project is read-only.

The application relative virtual path '~/' is not allowed here

Sep 28, 2009 at 5:54 PM

Hi Joel,

I am trying to use the DataFactoryFacade in a unit test...

       DataFactoryFacade dataFactoryFacade = mocks.DynamicMock<DataFactoryFacade>();

       using(mocks.Record())
       {
           Expect.Call(dataFactoryFacade.GetChildren(new PageReference())).Return(GetTestPageDataCollection());
       }
       using(mocks.Playback())
       {
           Presenter.GetPromoPanels(dataFactoryFacade);    
       }

        public void GetPromoPanels(DataFactoryFacade factory)
        {
            throw new NotImplementedException();
        }


As you can see I am trying to mack it and then playback a call. The method that gets called is not implemented yet. When I run this test I get the error listed below. Any ideas?

System.ArgumentException: The application relative virtual path '~/' is not allowed here.

at System.Web.VirtualPath.Create(String virtualPathVirtualPathOptions options)
at System.Web.Configuration.WebConfigurationManager.OpenWebConfigurationImpl(WebLevel webLevelConfigurationFileMap fileMapString pathString siteString locationSubPathString serverString userNameString passwordIntPtr userToken)
at System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(String path)
at EPiServer.Configuration.EPiServerSection.get_CurrentConfiguration()
at EPiServer.Configuration.EPiServerSection.get_Instance()
at EPiServer.DataFactory..cctor()

System.TypeInitializationException: The type initializer for 'EPiServer.DataFactory' threw an exception.

at EPiServer.DataFactory.get_Instance()
at EPiAbstractions.DataFactoryFacade..ctor()
at DataFactoryFacadeProxycea73b07fa79439da4889e6b9a058fcf..ctor(IInterceptor[] )

System.Exception: Exception in constructor: System.TypeInitializationException: The type initializer for 'EPiServer.DataFactory' threw an exception. ---> System.ArgumentException: The application relative virtual path '~/' is not allowed here.
   at System.Web.VirtualPath.Create(String virtualPath, VirtualPathOptions options)
   at System.Web.Configuration.WebConfigurationManager.OpenWebConfigurationImpl(WebLevel webLevel, ConfigurationFileMap fileMap, String path, String site, String locationSubPath, String server, String userName, String password, IntPtr userToken)
   at System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(String path)
   at EPiServer.Configuration.EPiServerSection.get_CurrentConfiguration()
   at EPiServer.Configuration.EPiServerSection.get_Instance()
   at EPiServer.DataFactory..cctor()
   --- End of inner exception stack trace ---
   at EPiServer.DataFactory.get_Instance()
   at EPiAbstractions.DataFactoryFacade..ctor()
   at DataFactoryFacadeProxycea73b07fa79439da4889e6b9a058fcf..ctor(IInterceptor[] )




Coordinator
Sep 29, 2009 at 8:31 AM

Hi Jet!

My guess is that you are making a method call that hasn't been mocked and as you are using a DynamicMock the actual method is executed.

Could you paste the entire class here? If you do so I can take a closer look.

Sep 29, 2009 at 8:53 AM

Hi Joel,

Here is the test fixture and the presenter class. Only namespaces and references ommited.

[TestFixture]
    public class PromoPanelContainerTests : AbstractPresenterTest<PromoPanelContainerPresenter>
    {
        /*
     
            Get the max number of panels that can be contained
            Throw an exception if the max num of panels is not a number
            Get children of promo panel type
            Should check publish dates and only display if publish dates are valid
            Display promo panels in correct sort order
            Call initialise on promo panels
      
       */

        protected override void Before_each_test()
        {
           

            //Overridden Setup method
            Presenter.CurrentPage = EPiServerPage.CreateBlankPage()
                .AddProperty<PropertyString>(PromoPanelContainerPresenter.PropertyNames.MAX_PANELS_THAT_CAN_BE_CONTAINED, GetValueBetweenZeroAndFive())
                .Instance();

            Presenter.View = mocks.StrictMock<IPromoPanelContainerView>();
        }

        private object GetValueBetweenZeroAndFive()
        {
            Random r = new Random((int) DateTime.Now.Ticks);
            return r.Next(0, 5);
        }

        [Test]
        public void Should_Get_Max_Number_Of_Panels_That_Can_Be_Contained()
        {
            Assert.AreEqual(Presenter.GetMaxNumberOfPanelsThatCanBeContained(), Int32.Parse(EPiServerUtils.GetPropertyString(Presenter.CurrentPage, PromoPanelContainerPresenter.PropertyNames.MAX_PANELS_THAT_CAN_BE_CONTAINED, false, true, false, true)));
        }

        [Test]
        public void Should_Throw_Exception_If_Max_Number_Of_Panels_That_Can_Be_Contained_Is_Not_An_Integer()
        {
             //Overridden Setup method
            Presenter.CurrentPage = EPiServerPage.CreateBlankPage()
                .AddProperty<PropertyString>(PromoPanelContainerPresenter.PropertyNames.MAX_PANELS_THAT_CAN_BE_CONTAINED, "Some None Integer Value")
                .Instance();

            Assert.Throws<UnableToParseIntegerFromPropertyException>(() => Presenter.GetMaxNumberOfPanelsThatCanBeContained());
        }

        [Test]
        public void Should_Check_Published_Dates_Are_Valid()
        {
            DataFactoryFacade dataFactoryFacade = mocks.StrictMock<DataFactoryFacade>();

            using(mocks.Record())
            {
                Expect.Call(dataFactoryFacade.GetChildren(Presenter.CurrentPage.PageLink)).Return(GetTestPageDataCollection());
 } using(mocks.Playback()) { Presenter.GetPromoPanels(dataFactoryFacade); } } private PageDataCollection GetTestPageDataCollection() { PageData childPageOne = EPiServerPage.CreateBlankPage() .AddProperty<PropertyDate>("StartPublish", DateTime.Now.AddDays(-2)) .AddProperty<PropertyDate>("StopPublish", DateTime.Now.AddDays(-1)) .Instance(); PageData childPageTwo = EPiServerPage.CreateBlankPage() .AddProperty<PropertyDate>("StartPublish", DateTime.Now.AddDays(-1)) .AddProperty<PropertyDate>("StopPublish", DateTime.Now.AddDays(1)) .Instance(); PageData childPageThree = EPiServerPage.CreateBlankPage() .AddProperty<PropertyDate>("StartPublish", DateTime.Now.AddDays(1)) .AddProperty<PropertyDate>("StopPublish", DateTime.Now.AddDays(2)) .Instance(); PageDataCollection pdc = new PageDataCollection {childPageOne, childPageTwo, childPageThree}; return pdc; } [Test] public void Test_Name() { throw new NotImplementedException(); //Arrange //Act //Assert } } public class PromoPanelContainerPresenter { #region Nested Classes /// <summary> /// Nested class to list property names for this page. /// </summary> public static class PropertyNames { public const string MAX_PANELS_THAT_CAN_BE_CONTAINED = "MaximumPanelsThatCanBeContained"; } #endregion public IPromoPanelContainerView View { get; set;} public PageData CurrentPage { get; set; } public int GetMaxNumberOfPanelsThatCanBeContainedOld() { string maxNumberString = EPiServerUtils.GetPropertyString(CurrentPage, PropertyNames.MAX_PANELS_THAT_CAN_BE_CONTAINED, false, true, false,true); int i; if (!Int32.TryParse(maxNumberString, out i)) throw new UnableToParseIntegerFromPropertyException("Unable to parse integer from EPiServer property", "Check value provided in the admin section", null); return i; } public int GetMaxNumberOfPanelsThatCanBeContained() { try { return EPiServerUtils.GetPropertyInt(CurrentPage, PropertyNames.MAX_PANELS_THAT_CAN_BE_CONTAINED, false, true, false, true); } catch (InvalidOperationException invalidOperationException) { throw new UnableToParseIntegerFromPropertyException("Unable to parse integer from EPiServer property", "Check value provided in the admin section", null); } } public void GetPromoPanels(DataFactoryFacade factory) { throw new NotImplementedException(); } }

public interface IPromoPanelContainerView
{

}
 

[TestFixture]
    public class PromoPanelContainerTests : AbstractPresenterTest<PromoPanelContainerPresenter>
    {
        /*
     
            Get the max number of panels that can be contained
            Throw an exception if the max num of panels is not a number
            Get children of promo panel type
            Should check publish dates and only display if publish dates are valid
            Display promo panels in correct sort order
            Call initialise on promo panels
      
      */

        // Private Constants for testing


        protected override void Before_each_test()
        {
           

            //Overridden Setup method
            Presenter.CurrentPage = EPiServerPage.CreateBlankPage()
                .AddProperty<PropertyString>(PromoPanelContainerPresenter.PropertyNames.MAX_PANELS_THAT_CAN_BE_CONTAINED, GetValueBetweenZeroAndFive())
                .Instance();

            Presenter.View = mocks.StrictMock<IPromoPanelContainerView>();
        }

        private object GetValueBetweenZeroAndFive()
        {
            Random r = new Random((int) DateTime.Now.Ticks);
            return r.Next(0, 5);
        }

        [Test]
        public void Should_Get_Max_Number_Of_Panels_That_Can_Be_Contained()
        {
            Assert.AreEqual(Presenter.GetMaxNumberOfPanelsThatCanBeContained(), Int32.Parse(EPiServerUtils.GetPropertyString(Presenter.CurrentPage, PromoPanelContainerPresenter.PropertyNames.MAX_PANELS_THAT_CAN_BE_CONTAINED, false, true, false, true)));
        }

        [Test]
        public void Should_Throw_Exception_If_Max_Number_Of_Panels_That_Can_Be_Contained_Is_Not_An_Integer()
        {
             //Overridden Setup method
            Presenter.CurrentPage = EPiServerPage.CreateBlankPage()
                .AddProperty<PropertyString>(PromoPanelContainerPresenter.PropertyNames.MAX_PANELS_THAT_CAN_BE_CONTAINED, "Dave")
                .Instance();

            Assert.Throws<UnableToParseIntegerFromPropertyException>(() => Presenter.GetMaxNumberOfPanelsThatCanBeContained());
        }

        [Test]
        public void Should_Check_Published_Dates_Are_Valid()
        {
            DataFactoryFacade dataFactoryFacade = mocks.StrictMock<DataFactoryFacade>();

            using(mocks.Record())
            {
                Expect.Call(dataFactoryFacade.GetChildren(new PageReference())).Return(GetTestPageDataCollection());
            }
            using(mocks.Playback())
            {
                Presenter.GetPromoPanels(dataFactoryFacade);    
            }
        }

        private PageDataCollection GetTestPageDataCollection()
        {
            PageData childPageOne = EPiServerPage.CreateBlankPage()
                .AddProperty<PropertyDate>("StartPublish", DateTime.Now.AddDays(-2))
                .AddProperty<PropertyDate>("StopPublish", DateTime.Now.AddDays(-1))
                .Instance();

            PageData childPageTwo = EPiServerPage.CreateBlankPage()
                .AddProperty<PropertyDate>("StartPublish", DateTime.Now.AddDays(-1))
                .AddProperty<PropertyDate>("StopPublish", DateTime.Now.AddDays(1))
                .Instance();

            PageData childPageThree = EPiServerPage.CreateBlankPage()
                .AddProperty<PropertyDate>("StartPublish", DateTime.Now.AddDays(1))
                .AddProperty<PropertyDate>("StopPublish", DateTime.Now.AddDays(2))
                .Instance();
            
            PageDataCollection pdc = new PageDataCollection {childPageOne, childPageTwo, childPageThree};

            return pdc;
        }

        [Test]
        public void Test_Name()
        {
            throw new NotImplementedException();

            //Arrange

            //Act

            //Assert
        }
    }

    public class UnableToParseIntegerFromPropertyException : ActionableException
    {
        public UnableToParseIntegerFromPropertyException(string message, string resolutionAction, Exception innerException) : base(message, resolutionAction, innerException)
        {
        }
    }

    public interface IPromoPanelContainerView
    {
    }

    public class PromoPanelContainerPresenter
    {
    
        #region Nested Classes
        /// <summary>
        /// Nested class to list property names for this page.
        /// </summary>
        public static class PropertyNames
        {
            public const string MAX_PANELS_THAT_CAN_BE_CONTAINED = "MaximumPanelsThatCanBeContained";
        }
        #endregion
    
        public IPromoPanelContainerView View { get; set;}
        public PageData CurrentPage { get; set; }

        public int GetMaxNumberOfPanelsThatCanBeContainedOld()
        {
            string maxNumberString = EPiServerUtils.GetPropertyString(CurrentPage, PropertyNames.MAX_PANELS_THAT_CAN_BE_CONTAINED, false, true, false,true);
            int i;
            if (!Int32.TryParse(maxNumberString, out i))
                throw new UnableToParseIntegerFromPropertyException("Unable to parse integer from EPiServer property", "Check value provided in the admin section", null);

            return i;
        }

        public int GetMaxNumberOfPanelsThatCanBeContained()
        {
            try
            {
                return EPiServerUtils.GetPropertyInt(CurrentPage, PropertyNames.MAX_PANELS_THAT_CAN_BE_CONTAINED, false, true, false, true);
            }
            catch (InvalidOperationException invalidOperationException)
            {
                throw new UnableToParseIntegerFromPropertyException("Unable to parse integer from EPiServer property", "Check value provided in the admin section", null);
            }
        }
        
        public void GetPromoPanels(DataFactoryFacade factory)
        {
            throw new NotImplementedException();
        }
 
[TestFixture]
    public class PromoPanelContainerTests : AbstractPresenterTest<PromoPanelContainerPresenter>
    {
        /*
     
            Get the max number of panels that can be contained
            Throw an exception if the max num of panels is not a number
            Get children of promo panel type
            Should check publish dates and only display if publish dates are valid
            Display promo panels in correct sort order
            Call initialise on promo panels
      
      */

        // Private Constants for testing


        protected override void Before_each_test()
        {
           

            //Overridden Setup method
            Presenter.CurrentPage = EPiServerPage.CreateBlankPage()
                .AddProperty<PropertyString>(PromoPanelContainerPresenter.PropertyNames.MAX_PANELS_THAT_CAN_BE_CONTAINED, GetValueBetweenZeroAndFive())
                .Instance();

            Presenter.View = mocks.StrictMock<IPromoPanelContainerView>();
        }

        private object GetValueBetweenZeroAndFive()
        {
            Random r = new Random((int) DateTime.Now.Ticks);
            return r.Next(0, 5);
        }

        [Test]
        public void Should_Get_Max_Number_Of_Panels_That_Can_Be_Contained()
        {
            Assert.AreEqual(Presenter.GetMaxNumberOfPanelsThatCanBeContained(), Int32.Parse(EPiServerUtils.GetPropertyString(Presenter.CurrentPage, PromoPanelContainerPresenter.PropertyNames.MAX_PANELS_THAT_CAN_BE_CONTAINED, false, true, false, true)));
        }

        [Test]
        public void Should_Throw_Exception_If_Max_Number_Of_Panels_That_Can_Be_Contained_Is_Not_An_Integer()
        {
             //Overridden Setup method
            Presenter.CurrentPage = EPiServerPage.CreateBlankPage()
                .AddProperty<PropertyString>(PromoPanelContainerPresenter.PropertyNames.MAX_PANELS_THAT_CAN_BE_CONTAINED, "Dave")
                .Instance();

            Assert.Throws<UnableToParseIntegerFromPropertyException>(() => Presenter.GetMaxNumberOfPanelsThatCanBeContained());
        }

        [Test]
        public void Should_Check_Published_Dates_Are_Valid()
        {
            DataFactoryFacade dataFactoryFacade = mocks.StrictMock<DataFactoryFacade>();

            using(mocks.Record())
            {
                Expect.Call(dataFactoryFacade.GetChildren(new PageReference())).Return(GetTestPageDataCollection());
            }
            using(mocks.Playback())
            {
                Presenter.GetPromoPanels(dataFactoryFacade);    
            }
        }

        private PageDataCollection GetTestPageDataCollection()
        {
            PageData childPageOne = EPiServerPage.CreateBlankPage()
                .AddProperty<PropertyDate>("StartPublish", DateTime.Now.AddDays(-2))
                .AddProperty<PropertyDate>("StopPublish", DateTime.Now.AddDays(-1))
                .Instance();

            PageData childPageTwo = EPiServerPage.CreateBlankPage()
                .AddProperty<PropertyDate>("StartPublish", DateTime.Now.AddDays(-1))
                .AddProperty<PropertyDate>("StopPublish", DateTime.Now.AddDays(1))
                .Instance();

            PageData childPageThree = EPiServerPage.CreateBlankPage()
                .AddProperty<PropertyDate>("StartPublish", DateTime.Now.AddDays(1))
                .AddProperty<PropertyDate>("StopPublish", DateTime.Now.AddDays(2))
                .Instance();
            
            PageDataCollection pdc = new PageDataCollection {childPageOne, childPageTwo, childPageThree};

            return pdc;
        }

        [Test]
        public void Test_Name()
        {
            throw new NotImplementedException();

            //Arrange

            //Act

            //Assert
        }
    }

    public class UnableToParseIntegerFromPropertyException : ActionableException
    {
        public UnableToParseIntegerFromPropertyException(string message, string resolutionAction, Exception innerException) : base(message, resolutionAction, innerException)
        {
        }
    }

    public interface IPromoPanelContainerView
    {
    }

    public class PromoPanelContainerPresenter
    {
    
        #region Nested Classes
        /// <summary>
        /// Nested class to list property names for this page.
        /// </summary>
        public static class PropertyNames
        {
            public const string MAX_PANELS_THAT_CAN_BE_CONTAINED = "MaximumPanelsThatCanBeContained";
        }
        #endregion
    
        public IPromoPanelContainerView View { get; set;}
        public PageData CurrentPage { get; set; }

        public int GetMaxNumberOfPanelsThatCanBeContainedOld()
        {
            string maxNumberString = EPiServerUtils.GetPropertyString(CurrentPage, PropertyNames.MAX_PANELS_THAT_CAN_BE_CONTAINED, false, true, false,true);
            int i;
            if (!Int32.TryParse(maxNumberString, out i))
                throw new UnableToParseIntegerFromPropertyException("Unable to parse integer from EPiServer property", "Check value provided in the admin section", null);

            return i;
        }

        public int GetMaxNumberOfPanelsThatCanBeContained()
        {
            try
            {
                return EPiServerUtils.GetPropertyInt(CurrentPage, PropertyNames.MAX_PANELS_THAT_CAN_BE_CONTAINED, false, true, false, true);
            }
            catch (InvalidOperationException invalidOperationException)
            {
                throw new UnableToParseIntegerFromPropertyException("Unable to parse integer from EPiServer property", "Check value provided in the admin section", null);
            }
        }
        
        public void GetPromoPanels(DataFactoryFacade factory)
        {
            throw new NotImplementedException();
        }
    }	
   }   
Coordinator
Sep 29, 2009 at 9:02 AM

Hey again!

Try using the interface IDataFactoryFacade instead (available in version 1.1 of EPiAbstractions). While all methods in the concrete class DataFactoryFacade can be mocked it is dependent on the real DataFactory.Instance as it registers events with it. That's a problem in EPiAbstractions that I'll see if I can fix, but using the interface is a simple and good workaround.

/Joel

Oct 14, 2009 at 1:33 PM

Thanks Joel, That worked straight away and we have been busy using it ever since and I forgot to come back and than you for your help in this.

a big THANKS to you.