Here is a brief overview of what I have done to get tests passing. In most cases, we are dealing with the absence of the HttpContext or Application Domain values that are normally available during the application run time. I won't repeat what I have said earlier in the post, but I am hoping that there is enough stuff here to point the interested reader in the right direction. It is also important that I have gone for quick fixes, and in some instances these come with caveats, such as subtly altering application behaviour.
//umbraco.Test/SetUpUtilities.cs
using System;
using System.Collections.Specialized;
using System.Xml;
using System.Web;
using System.Web.Caching;
using umbraco.BusinessLogic;
namespace umbraco.Test
{
public class SetUpUtilities
{
public SetUpUtilities () {}
private const string _umbracoDbDSN = "server=127.0.0.1;database=UMBRACO_DB;user id=USER_ID;password=PASSWORD;datalayer=MySql";
private const string _umbracoConfigFile = "<PATH-TO-APPLICATION>/config/umbracoSettings.config";
private const string _dynamicBase = "<PATH-TO-ASSEMBLY-CACHE> (e.g., /tmp/USER_ID-temp-aspnet-0)";
public static NameValueCollection GetAppSettings()
{
NameValueCollection appSettings = new NameValueCollection();
//add application settings
appSettings.Add("umbracoDbDSN", _umbracoDbDSN);
return appSettings;
}
public static void AddUmbracoConfigFileToHttpCache()
{
XmlDocument temp = new XmlDocument();
XmlTextReader settingsReader = new XmlTextReader(_umbracoConfigFile);
temp.Load(settingsReader);
HttpRuntime.Cache.Insert("umbracoSettingsFile", temp,
new CacheDependency(_umbracoConfigFile));
}
public static void RemoveUmbracoConfigFileFromHttpCache()
{
HttpRuntime.Cache.Remove("umbracoSettingsFile");
}
public static void InitConfigurationManager()
{
ConfigurationManagerService.ConfigManager = new ConfigurationManagerTest(SetUpUtilities.GetAppSettings());
}
public static void InitAppDomainDynamicBase()
{
AppDomain.CurrentDomain.SetDynamicBase(_dynamicBase); //(Obsolete but works...)
//AppDomain.CurrentDomain.SetupInformation.DynamicBase = _dynamicBase;
}
}
}
//sample test file set-up
...
private User m_User;
[TestFixtureSetUp]
public void InitTestFixture()
{
SetUpUtilities.InitConfigurationManager();
m_User = new User(0);
SetUpUtilities.InitAppDomainDynamicBase();
}
[SetUp]
public void MyTestInitialize()
{
SetUpUtilities.AddUmbracoConfigFileToHttpCache();
...
}
[TearDown]
public void MyTestCleanup()
{
...
SetUpUtilities.RemoveUmbracoConfigFileFromHttpCache();
}
...
//couple of hacks...
.../umbraco/businesslogic/IO/MultiPlatformHelper.cs,
public static string MapUnixPath(string path)
{
string filePath = path;
if (filePath.StartsWith("~"))
filePath = IOHelper.ResolveUrl(filePath);
filePath = IOHelper.MapPath(filePath, System.Web.HttpContext.Current != null);
return filePath;
}
.../umbraco/businesslogic/IO/IOHelper.cs,
private static string getRootDirectorySafe()
{
if (!String.IsNullOrEmpty(m_rootDir))
{
return m_rootDir;
}
string baseDirectory =
System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().CodeBase.Substring(8));
m_rootDir = baseDirectory.Substring(0, baseDirectory.LastIndexOf("bin") - 1);
//changed for tests ck, 9/9/12
if (MultiPlatformHelper.IsUnix && !m_rootDir.StartsWith(IOHelper.DirSepChar.ToString()))
m_rootDir = IOHelper.DirSepChar + m_rootDir;
return m_rootDir;
}
Language_Get_By_Culture_Code terminates with error:
linq operation is not valid due to the current state of the object
do:
Language.cs (161) Replace SingleOrDefault() with FirstOrDefault()
templates, stylesheet tests fail: create directories (masterpages, css) in test project.
No comments:
Post a Comment
Note: only a member of this blog may post a comment.