Created
October 26, 2012 21:08
-
-
Save carolynvs/3961521 to your computer and use it in GitHub Desktop.
Demonstrates how to unit test ASP.NET MVC bundles
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head runat="server"> | |
@Styles.Render(Bundles.Styles.Common) | |
@Scripts.Render(Bundles.Scripts.Common) | |
// ... | |
</head> | |
// ... | |
</html> | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Web.Optimization; | |
public class BundleConfig | |
{ | |
public static void RegisterBundles(BundleCollection bundles) | |
{ | |
bundles.Add(new StyleBundle(Bundles.Styles.Common) | |
.Include("~/Content/normalize.css", | |
"~/Content/site.css")); | |
bundles.Add(new ScriptBundle(Bundles.Scripts.Common) | |
.Include("~/Scripts/jquery.js", | |
"~/Scripts/knockout.js", | |
"~/Scripts/plugins.js")); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Linq; | |
using System.Web.Optimization; | |
using System.Collections.Generic; | |
using MvcContrib.TestHelper.Fakes; | |
using NUnit.Framework; | |
namespace PieCharts.Tests | |
{ | |
[TestFixture] | |
public class BundleConfigFixture | |
{ | |
private List<Bundle> _bundles; | |
private BundleContext _mockBundleContext; | |
[SetUp] | |
public void Setup() | |
{ | |
_mockBundleContext = new BundleContext(new FakeHttpContext(string.Empty), new BundleCollection(), string.Empty); | |
BundleTable.MapPathMethod = MapBundleItemPath; // Mock mapping a bundle item's virtual path to a physical path | |
_bundles = BundleConfig.GetBundles(); | |
} | |
/// <summary> | |
/// Converts a bundle item's virtual path to a physical path | |
/// </summary> | |
/// <param name="item">The bundle item virtual path</param> | |
/// <returns>The physical path of the bundl item</returns> | |
private static string MapBundleItemPath(string item) | |
{ | |
// todo: Replace this with whatever logic you need to map from your virtual paths to a physical path | |
// Strip the ~ and switch from / to \ | |
item = item.Replace("~", string.Empty).Replace("/", @"\"); | |
// Join the item virtal path with the location of your MVC project | |
const string pathToMvcProject = @"C:\MySolution\MyMVCProject"; | |
return string.Format("{0}{1}", pathToMvcProject, item); | |
} | |
[Test(Description = "All bundles should have at least one file included")] | |
public void AllBundlesHaveAtLeastOneFileIncluded() | |
{ | |
var emptyBundles = _bundles | |
.Where(bundle => !bundle.EnumerateFiles(_mockBundleContext).Any()) | |
.Select(bundle => bundle.Path) | |
.ToList(); | |
Assert.IsEmpty(emptyBundles, string.Format("The following bundles are empty: {0}", string.Join(", ", emptyBundles))); | |
} | |
[Test(Description = "Explicitly validate that every file was successfully included in the Common Styles bundle")] | |
public void CommonStylesBundle() | |
{ | |
List<string> files = GetFilesInBundle(Bundles.Styles.Common); | |
List<string> expectedFiles = BuildExpectedFiles("normalize.css", "site.css"); | |
CollectionAssert.AreEquivalent(expectedFiles, files); | |
} | |
[Test(Description = "Explicitly validate that every file was successfully included in the Common Scripts bundle")] | |
public void CommonScriptsBundle() | |
{ | |
List<string> files = GetFilesInBundle(Bundles.Scripts.Common); | |
List<string> expectedFiles = BuildExpectedFiles("jquery.js", "knockout.js", "plugins.js"); | |
CollectionAssert.AreEquivalent(expectedFiles, files); | |
} | |
/// <summary> | |
/// Builds a list of lower case file names in ascending order | |
/// </summary> | |
private List<string> BuildExpectedFiles(params string[] files) | |
{ | |
return files.Select(x => x.ToLower()) | |
.OrderBy(x => x) | |
.ToList(); | |
} | |
/// <summary> | |
/// Builds the list of file names in the specified bundle | |
/// Names are lower cased in ascending order | |
/// </summary> | |
private List<string> GetFilesInBundle(string bundleName) | |
{ | |
var bundle = _bundles.First(x => x.Path == bundleName); | |
return bundle.EnumerateFiles(_mockBundleContext) | |
.Select(x => x.Name.ToLower()) | |
.OrderBy(x => x) | |
.ToList(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class Bundles | |
{ | |
public static class Styles | |
{ | |
public const string Common = "~/styles/common"; | |
} | |
public static class Scripts | |
{ | |
public const string Common = "~/scripts/common"; | |
} | |
} |
Where does this BundleTable.MapPathMethod come from? I'm using MVC4 and the method doesn't seem to exist.
The test AllBundlesHaveAtLeastOneFileIncluded is stupid, what if you have a bundle which uses transforms to populate the bundle content, this test will fail.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Read my post on how to unit test your ASP.NET MVC bundles