From b6f7d9903974f0b929046f91d0ecfff4a774a792 Mon Sep 17 00:00:00 2001 From: Shannon Deminick Date: Thu, 11 Oct 2012 08:32:06 +0500 Subject: [PATCH] Fixed why all the unit tests were failing. Ported over a couple of unit tests from the legacy test project to see how easy it would be and it looks pretty easy, just need the propery initialization data for the tests. Currently ported over the Dictionary biz logic tests.... most fail but pretty sure it's due to init data. --- .../BusinessLogic}/DictionaryTest.cs | 121 ++++++++---------- .../ContentStores/PublishContentStoreTests.cs | 3 +- .../Umbraco.Tests/IO}/IOHelperTest.cs | 66 ++-------- src/Umbraco.Tests/TestHelpers/BaseWebTest.cs | 8 +- src/Umbraco.Tests/Umbraco.Tests.csproj | 2 + test/umbraco.Test/Umbraco.LegacyTests.csproj | 2 - 6 files changed, 69 insertions(+), 133 deletions(-) rename {test/umbraco.Test => src/Umbraco.Tests/BusinessLogic}/DictionaryTest.cs (85%) rename {test/umbraco.Test => src/Umbraco.Tests/IO}/IOHelperTest.cs (58%) diff --git a/test/umbraco.Test/DictionaryTest.cs b/src/Umbraco.Tests/BusinessLogic/DictionaryTest.cs similarity index 85% rename from test/umbraco.Test/DictionaryTest.cs rename to src/Umbraco.Tests/BusinessLogic/DictionaryTest.cs index b217c11ee2..a73e085e87 100644 --- a/test/umbraco.Test/DictionaryTest.cs +++ b/src/Umbraco.Tests/BusinessLogic/DictionaryTest.cs @@ -1,30 +1,39 @@ -using umbraco.cms.businesslogic; -using Microsoft.VisualStudio.TestTools.UnitTesting; +using NUnit.Framework; +using Umbraco.Core; +using Umbraco.Tests.TestHelpers; +using umbraco.cms.businesslogic; using System; using System.Xml; using umbraco.cms.businesslogic.language; using umbraco.BusinessLogic; using System.Linq; -namespace Umbraco.LegacyTests +namespace Umbraco.Tests.BusinessLogic { - + //TODO: This was ported over from the previous unit tests, need to make them work now :) /// ///This is a test class for Dictionary_DictionaryItemTest and is intended ///to contain all Dictionary_DictionaryItemTest Unit Tests /// - [TestClass()] - public class DictionaryTest + [TestFixture] + public class DictionaryTest : BaseWebTest { - [TestMethod()] + public override void Initialize() + { + base.Initialize(); + + CreateNew(); + } + + [Test()] public void Dictionary_Get_Top_Level_Items() { var items = Dictionary.getTopMostItems; var d = CreateNew(); - Assert.AreEqual(items.Count() + 1, Dictionary.getTopMostItems.Count()); + Assert.AreEqual(items.Count() + 1, Dictionary.getTopMostItems.Count()); DeleteItem(d); } @@ -33,7 +42,7 @@ namespace Umbraco.LegacyTests /// Creates a new dictionary entry, adds values for all languages assigned, then deletes the /// entry and ensure that all other data is gone too. /// - [TestMethod()] + [Test()] public void Dictionary_Create_Add_Text_And_Delete() { var d = CreateNew(); @@ -55,7 +64,7 @@ namespace Umbraco.LegacyTests /// ///A test for IsTopMostItem /// - [TestMethod()] + [Test()] public void Dictionary_IsTopMostItem() { var parent = CreateNew(); @@ -64,7 +73,7 @@ namespace Umbraco.LegacyTests var childId = Dictionary.DictionaryItem.addKey("Test" + Guid.NewGuid().ToString("N"), "", parent.key); Assert.IsTrue(childId > 0); var child = new Dictionary.DictionaryItem(childId); - Assert.IsInstanceOfType(child, typeof(Dictionary.DictionaryItem)); + Assert.IsTrue(TypeHelper.IsTypeAssignableFrom(child)); Assert.IsTrue(parent.IsTopMostItem()); Assert.IsFalse(child.IsTopMostItem()); @@ -76,7 +85,7 @@ namespace Umbraco.LegacyTests /// /// Test the Parent and Children properties and ensures that the relationships work both ways /// - [TestMethod()] + [Test()] public void Dictionary_Parent_Child_Relationship() { var parent = CreateNew(); @@ -85,7 +94,7 @@ namespace Umbraco.LegacyTests var childId = Dictionary.DictionaryItem.addKey("Test" + Guid.NewGuid().ToString("N"), "", parent.key); Assert.IsTrue(childId > 0); var child = new Dictionary.DictionaryItem(childId); - Assert.IsInstanceOfType(child, typeof(Dictionary.DictionaryItem)); + Assert.IsTrue(TypeHelper.IsTypeAssignableFrom(child)); //set the parent relationship Assert.AreEqual(parent.id, child.Parent.id); @@ -94,7 +103,7 @@ namespace Umbraco.LegacyTests //test the child relationship Assert.IsTrue(parent.hasChildren); - Assert.AreEqual(1, parent.Children.Length); + Assert.AreEqual(1, parent.Children.Length); Assert.AreEqual(child.id, parent.Children.First().id); Assert.AreEqual(child.key, parent.Children.First().key); Assert.AreEqual(child.UniqueId, parent.Children.First().UniqueId); @@ -106,7 +115,7 @@ namespace Umbraco.LegacyTests /// /// Deletes a parent with existing children and ensures they are all gone. /// - [TestMethod()] + [Test()] public void Dictionary_Delete_Parent_With_Children() { var parent = CreateNew(); @@ -115,16 +124,16 @@ namespace Umbraco.LegacyTests var childId1 = Dictionary.DictionaryItem.addKey("Test" + Guid.NewGuid().ToString("N"), "", parent.key); Assert.IsTrue(childId1 > 0); var child1 = new Dictionary.DictionaryItem(childId1); - Assert.IsInstanceOfType(child1, typeof(Dictionary.DictionaryItem)); + Assert.IsTrue(TypeHelper.IsTypeAssignableFrom(child1)); //create a child var childId2 = Dictionary.DictionaryItem.addKey("Test" + Guid.NewGuid().ToString("N"), "", parent.key); Assert.IsTrue(childId2 > 0); var child2 = new Dictionary.DictionaryItem(childId2); - Assert.IsInstanceOfType(child2, typeof(Dictionary.DictionaryItem)); + Assert.IsTrue(TypeHelper.IsTypeAssignableFrom(child2)); Assert.IsTrue(parent.hasChildren); - Assert.AreEqual(2, parent.Children.Length); + Assert.AreEqual(2, parent.Children.Length); DeleteItem(parent); @@ -157,7 +166,7 @@ namespace Umbraco.LegacyTests /// /// Guid constructor test /// - [TestMethod()] + [Test()] public void Dictionary_Contructor_Guid() { var d = CreateNew(); @@ -174,7 +183,7 @@ namespace Umbraco.LegacyTests /// /// key constructor test /// - [TestMethod()] + [Test()] public void Dictionary_Contructor_Key() { var d = CreateNew(); @@ -191,7 +200,7 @@ namespace Umbraco.LegacyTests /// ///A test for ToXml /// - [TestMethod()] + [Test()] public void Dictionary_ToXml() { var d = CreateNew(); @@ -200,7 +209,7 @@ namespace Umbraco.LegacyTests var childId = Dictionary.DictionaryItem.addKey("Test" + Guid.NewGuid().ToString("N"), "", d.key); Assert.IsTrue(childId > 0); var child = new Dictionary.DictionaryItem(childId); - Assert.IsInstanceOfType(child, typeof(Dictionary.DictionaryItem)); + Assert.IsTrue(TypeHelper.IsTypeAssignableFrom(child)); var xml = new XmlDocument(); @@ -218,7 +227,7 @@ namespace Umbraco.LegacyTests /// ///A test to change the key of an element /// - [TestMethod()] + [Test()] public void Dictionary_Change_Key() { //System.Diagnostics.Debugger.Break(); @@ -247,14 +256,14 @@ namespace Umbraco.LegacyTests /// /// Tries to create a duplicate key and ensures it's not possible. /// - [TestMethod()] + [Test()] public void Dictionary_Attempt_Duplicate_Key() { var key = "Test" + Guid.NewGuid().ToString("N"); var d1Id = Dictionary.DictionaryItem.addKey(key, ""); Assert.IsTrue(d1Id > 0); var d1 = new Dictionary.DictionaryItem(d1Id); - Assert.IsInstanceOfType(d1, typeof(Dictionary.DictionaryItem)); + Assert.IsTrue(TypeHelper.IsTypeAssignableFrom(d1)); var alreadyExists = false; try @@ -273,13 +282,14 @@ namespace Umbraco.LegacyTests } #region Private methods + private Dictionary.DictionaryItem CreateNew() { var id = Dictionary.DictionaryItem.addKey("Test" + Guid.NewGuid().ToString("N"), ""); Assert.IsTrue(id > 0); var d = new Dictionary.DictionaryItem(id); - Assert.IsInstanceOfType(d, typeof(Dictionary.DictionaryItem)); + Assert.IsTrue(TypeHelper.IsTypeAssignableFrom(d)); return d; } @@ -314,7 +324,7 @@ namespace Umbraco.LegacyTests ///// /////A test for Import ///// - //[TestMethod()] + //[Test()] //public void ImportTest() //{ // XmlNode xmlData = null; // TODO: Initialize to an appropriate value @@ -329,7 +339,7 @@ namespace Umbraco.LegacyTests ///// /////A test for Import ///// - //[TestMethod()] + //[Test()] //public void ImportTest1() //{ // XmlNode xmlData = null; // TODO: Initialize to an appropriate value @@ -345,7 +355,7 @@ namespace Umbraco.LegacyTests ///// /////A test for Save ///// - //[TestMethod()] + //[Test()] //public void SaveTest() //{ // Guid id = new Guid(); // TODO: Initialize to an appropriate value @@ -359,7 +369,7 @@ namespace Umbraco.LegacyTests ///// /////A test for Value ///// - //[TestMethod()] + //[Test()] //public void ValueTest() //{ // Guid id = new Guid(); // TODO: Initialize to an appropriate value @@ -375,7 +385,7 @@ namespace Umbraco.LegacyTests ///// /////A test for Value ///// - //[TestMethod()] + //[Test()] //public void ValueTest1() //{ // Guid id = new Guid(); // TODO: Initialize to an appropriate value @@ -390,7 +400,7 @@ namespace Umbraco.LegacyTests ///// /////A test for addKey ///// - //[TestMethod()] + //[Test()] //public void addKeyTest() //{ // string key = string.Empty; // TODO: Initialize to an appropriate value @@ -405,7 +415,7 @@ namespace Umbraco.LegacyTests ///// /////A test for addKey ///// - //[TestMethod()] + //[Test()] //public void addKeyTest1() //{ // string key = string.Empty; // TODO: Initialize to an appropriate value @@ -421,7 +431,7 @@ namespace Umbraco.LegacyTests ///// /////A test for delete ///// - //[TestMethod()] + //[Test()] //public void deleteTest() //{ // Guid id = new Guid(); // TODO: Initialize to an appropriate value @@ -433,7 +443,7 @@ namespace Umbraco.LegacyTests ///// /////A test for hasKey ///// - //[TestMethod()] + //[Test()] //public void hasKeyTest() //{ // string key = string.Empty; // TODO: Initialize to an appropriate value @@ -447,7 +457,7 @@ namespace Umbraco.LegacyTests ///// /////A test for setValue ///// - //[TestMethod()] + //[Test()] //public void setValueTest() //{ // Guid id = new Guid(); // TODO: Initialize to an appropriate value @@ -461,7 +471,7 @@ namespace Umbraco.LegacyTests ///// /////A test for setValue ///// - //[TestMethod()] + //[Test()] //public void setValueTest1() //{ // Guid id = new Guid(); // TODO: Initialize to an appropriate value @@ -474,7 +484,7 @@ namespace Umbraco.LegacyTests ///// /////A test for Children ///// - //[TestMethod()] + //[Test()] //public void ChildrenTest() //{ // Guid id = new Guid(); // TODO: Initialize to an appropriate value @@ -487,7 +497,7 @@ namespace Umbraco.LegacyTests ///// /////A test for Parent ///// - //[TestMethod()] + //[Test()] //public void ParentTest() //{ // Guid id = new Guid(); // TODO: Initialize to an appropriate value @@ -500,7 +510,7 @@ namespace Umbraco.LegacyTests ///// /////A test for hasChildren ///// - //[TestMethod()] + //[Test()] //public void hasChildrenTest() //{ // Guid id = new Guid(); // TODO: Initialize to an appropriate value @@ -513,7 +523,7 @@ namespace Umbraco.LegacyTests ///// /////A test for id ///// - //[TestMethod()] + //[Test()] //public void idTest() //{ // Guid id = new Guid(); // TODO: Initialize to an appropriate value @@ -526,34 +536,5 @@ namespace Umbraco.LegacyTests #endregion - #region Initialize and Cleanup - // - //You can use the following additional attributes as you write your tests: - // - //Use ClassInitialize to run code before running the first test in the class - //[ClassInitialize()] - //public static void MyClassInitialize(TestContext testContext) - //{ - //} - // - //Use ClassCleanup to run code after all tests in a class have run - //[ClassCleanup()] - //public static void MyClassCleanup() - //{ - //} - // - //Use TestInitialize to run code before running each test - //[TestInitialize()] - //public void MyTestInitialize() - //{ - //} - // - //Use TestCleanup to run code after each test has run - //[TestCleanup()] - //public void MyTestCleanup() - //{ - //} - // - #endregion } } diff --git a/src/Umbraco.Tests/ContentStores/PublishContentStoreTests.cs b/src/Umbraco.Tests/ContentStores/PublishContentStoreTests.cs index d8f7831e3e..e4e19a6b18 100644 --- a/src/Umbraco.Tests/ContentStores/PublishContentStoreTests.cs +++ b/src/Umbraco.Tests/ContentStores/PublishContentStoreTests.cs @@ -5,10 +5,9 @@ using Umbraco.Core; using Umbraco.Tests.TestHelpers; using Umbraco.Web; using Umbraco.Web.Routing; -using umbraco; using umbraco.BusinessLogic; -namespace Umbraco.Tests +namespace Umbraco.Tests.ContentStores { [TestFixture] public class PublishContentStoreTests diff --git a/test/umbraco.Test/IOHelperTest.cs b/src/Umbraco.Tests/IO/IOHelperTest.cs similarity index 58% rename from test/umbraco.Test/IOHelperTest.cs rename to src/Umbraco.Tests/IO/IOHelperTest.cs index ab4d382e95..d23d5cba5f 100644 --- a/test/umbraco.Test/IOHelperTest.cs +++ b/src/Umbraco.Tests/IO/IOHelperTest.cs @@ -1,7 +1,7 @@ -using umbraco.IO; -using Microsoft.VisualStudio.TestTools.UnitTesting; +using NUnit.Framework; +using Umbraco.Core.IO; -namespace Umbraco.LegacyTests +namespace Umbraco.Tests.IO { @@ -9,64 +9,14 @@ namespace Umbraco.LegacyTests ///This is a test class for IOHelperTest and is intended ///to contain all IOHelperTest Unit Tests /// - [TestClass()] + [TestFixture()] public class IOHelperTest { - - private TestContext testContextInstance; - - /// - ///Gets or sets the test context which provides - ///information about and functionality for the current test run. - /// - public TestContext TestContext - { - get - { - return testContextInstance; - } - set - { - testContextInstance = value; - } - } - - #region Additional test attributes - // - //You can use the following additional attributes as you write your tests: - // - //Use ClassInitialize to run code before running the first test in the class - //[ClassInitialize()] - //public static void MyClassInitialize(TestContext testContext) - //{ - //} - // - //Use ClassCleanup to run code after all tests in a class have run - //[ClassCleanup()] - //public static void MyClassCleanup() - //{ - //} - // - //Use TestInitialize to run code before running each test - //[TestInitialize()] - //public void MyTestInitialize() - //{ - //} - // - //Use TestCleanup to run code after each test has run - //[TestCleanup()] - //public void MyTestCleanup() - //{ - //} - // - #endregion - - /// ///A test for MapPath verifying that HttpContext method (which includes vdirs) matches non-HttpContext method /// - [TestMethod()] + [Test] public void IOHelper_MapPathTestVDirTraversal() { //System.Diagnostics.Debugger.Break(); @@ -84,9 +34,9 @@ namespace Umbraco.LegacyTests Assert.AreEqual(IOHelper.MapPath(SystemDirectories.Root, true), IOHelper.MapPath(SystemDirectories.Root, false)); Assert.AreEqual(IOHelper.MapPath(SystemDirectories.Scripts, true), IOHelper.MapPath(SystemDirectories.Scripts, false)); Assert.AreEqual(IOHelper.MapPath(SystemDirectories.Umbraco, true), IOHelper.MapPath(SystemDirectories.Umbraco, false)); - Assert.AreEqual(IOHelper.MapPath(SystemDirectories.Umbraco_client, true), IOHelper.MapPath(SystemDirectories.Umbraco_client, false)); - Assert.AreEqual(IOHelper.MapPath(SystemDirectories.Usercontrols, true), IOHelper.MapPath(SystemDirectories.Usercontrols, false)); - Assert.AreEqual(IOHelper.MapPath(SystemDirectories.Webservices, true), IOHelper.MapPath(SystemDirectories.Webservices, false)); + Assert.AreEqual(IOHelper.MapPath(SystemDirectories.UmbracoClient, true), IOHelper.MapPath(SystemDirectories.UmbracoClient, false)); + Assert.AreEqual(IOHelper.MapPath(SystemDirectories.UserControls, true), IOHelper.MapPath(SystemDirectories.UserControls, false)); + Assert.AreEqual(IOHelper.MapPath(SystemDirectories.WebServices, true), IOHelper.MapPath(SystemDirectories.WebServices, false)); Assert.AreEqual(IOHelper.MapPath(SystemDirectories.Xslt, true), IOHelper.MapPath(SystemDirectories.Xslt, false)); } } diff --git a/src/Umbraco.Tests/TestHelpers/BaseWebTest.cs b/src/Umbraco.Tests/TestHelpers/BaseWebTest.cs index 1b1076cb08..2009d7d683 100644 --- a/src/Umbraco.Tests/TestHelpers/BaseWebTest.cs +++ b/src/Umbraco.Tests/TestHelpers/BaseWebTest.cs @@ -1,3 +1,4 @@ +using System; using System.IO; using System.Web.Routing; using System.Xml; @@ -14,7 +15,7 @@ using umbraco.cms.businesslogic.template; namespace Umbraco.Tests.TestHelpers { - [TestFixture] + [TestFixture, RequiresSTA] public abstract class BaseWebTest { @@ -22,6 +23,9 @@ namespace Umbraco.Tests.TestHelpers public virtual void Initialize() { TestHelper.SetupLog4NetForTests(); + + AppDomain.CurrentDomain.SetData("DataDirectory", TestHelper.CurrentAssemblyDirectory); + if (RequiresDbSetup) TestHelper.InitializeDatabase(); Resolution.Freeze(); @@ -36,6 +40,8 @@ namespace Umbraco.Tests.TestHelpers [TearDown] public virtual void TearDown() { + AppDomain.CurrentDomain.SetData("DataDirectory", null); + //reset the app context ApplicationContext.Current = null; Resolution.IsFrozen = false; diff --git a/src/Umbraco.Tests/Umbraco.Tests.csproj b/src/Umbraco.Tests/Umbraco.Tests.csproj index e8893f92a9..b389798cf8 100644 --- a/src/Umbraco.Tests/Umbraco.Tests.csproj +++ b/src/Umbraco.Tests/Umbraco.Tests.csproj @@ -53,10 +53,12 @@ + + diff --git a/test/umbraco.Test/Umbraco.LegacyTests.csproj b/test/umbraco.Test/Umbraco.LegacyTests.csproj index 2876f15ebd..942d01a6a4 100644 --- a/test/umbraco.Test/Umbraco.LegacyTests.csproj +++ b/test/umbraco.Test/Umbraco.LegacyTests.csproj @@ -122,10 +122,8 @@ Properties\SolutionInfo.cs - -