Reading time: 6 min read
Complete Unit Testing In Sitecore With Sitecore.Context And App_Config
Feels Like Magic
Start typing to search...
- App_Config
- Include
- Prefetch
- Security
- Use *Shift* to highlight all files (not folders)
- Note the down arrow beside "Add" > click > Select **Add As Link**
Repeat these step with the *App_Config/Include*, *App_Config/Prefetch* & *App_Config/Security* folders respectively.
- Search for **nunit** using the search box. Add it.
- Rock and roll.
- Copy & paste the below code.
- See inline comments for a brief explanation
**TestSetupFixture.cs**
using System;
using System.IO;
using NUnit.Framework;
using Sitecore;
using Sitecore.Configuration;
using Sitecore.Globalization;
using Sitecore.SecurityModel;
namespace Fishtank.IntegrationTests
{
// In NUnit, this ensures it will run before any/all tests are executed
[SetUpFixture]
public class TestSetupFixture
{
public static SecurityDisabler _disabler;
[SetUp]
public void SetupTest()
{
try
{
// This grounds Sitecore in the current directory so when
// Sitecore.IO.FileUtil.MapPath runs, it can find the files.
State.HttpRuntime.AppDomainAppPath = Directory.GetCurrentDirectory();
// This static. Allows it live, avoiding garbage collection
_disabler = new SecurityDisabler();
// If you need to run pipelines do it hear.
//CorePipeline.Run("initialize", new PipelineArgs());
// Set any properties you need in content
Context.SetLanguage(Language.Parse("en"), false);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
throw;
}
}
}
}
If you don't use NUnit, you'll have to find an equivalent methodology. Or simply run the above code manually at the start of your tests.
using NUnit.Framework;
namespace Fishtank.IntegrationTests
{
[TestFixture]
public class SimpleTests
{
[Test]
public void ShouldFindTheHomeNode()
{
// Get getfishtank is declared under:
// /App_Config/Include/SiteDefinition.getfishtank.ca.config
Sitecore.Context.SetActiveSite("getfishtank");
// Pull the start path of the site
string startPath = Sitecore.Context.Site.StartPath;
// Pull the database name
string databaseName = Sitecore.Context.Site.Database.Name;
// Load the web database, and get item
var db = Sitecore.Data.Database.GetDatabase("web");
var item = db.GetItem("/sitecore/content/fishtank/home");
// Found the home node
Assert.That(item, Is.Not.Null);
// Paths of the home items match
Assert.That(startPath.ToLower(), Is.EqualTo(item.Paths.FullPath.ToLower()));
// Database name pulled from context matches too
Assert.That(databaseName, Is.EqualTo(db.Name));
}
}
}
Almost there!
An all encompassing screenshot showing:
- Visual Studio
- Open *SimpleTest.cs* test file
- Open site defintion file, */App_Config/Include/SiteDefinition.getfishtank.ca.config*
- NUnit test runner window showing a **passed test**
- **Properties** window showing that the linked config resides in */App_Config/Include/*.
If you this is your problem, you'll see these errors:
TestFixtureSetUp failed in TestSetupFixture Exception doesn't have a stacktrace
Could not find a part of the path 'C:\getfishtank\Fishtank.IntegrationTests\bin\Debug\app_config\prototypes.config'.