patterncsharpMinor
Unit tests for the SquishIt framework
Viewed 0 times
thesquishittestsforframeworkunit
Problem
For a little background, I'm a contributor to SquishIt and decided that I should start cleaning up the mess of unit tests (my updated version of one set of tests vs. original set of tests). After tackling only part of the "rewrite", I figured it would be a good opportunity to learn, try, and understand BDD style testing to hopefully help improve the unit tests being written and ran. I decided to go with NSpec after taking a look at some of the choices out there.
My problem is simply trying to break the tests down into the appropriate "groupings" that logically make sense and "conform" to xSpec. Here is what I've done so far and passes all tests as expected:
How can I improve this or am I already on the right track?
My problem is simply trying to break the tests down into the appropriate "groupings" that logically make sense and "conform" to xSpec. Here is what I've done so far and passes all tests as expected:
using NSpec;
using SquishIt.Framework;
using SquishIt.Framework.JavaScript;
namespace SquishIt.Tests
{
class describe_JavaScriptBundle : nspec
{
public JavaScriptBundle bundle;
public void AddTest1JS()
{
bundle.Add("test.js");
}
}
class when_I_create_a_bundle : describe_JavaScriptBundle
{
void before_each()
{
if (bundle == null)
{
bundle = Bundle.JavaScript();
}
}
void and_add_no_files()
{
it["GroupBundles containskey default"] = () => bundle.GroupBundles.ContainsKey("default").is_true();
it["has no Assets"] = () => bundle.GroupBundles["default"].Assets.Count.Is(0);
}
}
class then_I_add_a_file : when_I_create_a_bundle
{
void before_each()
{
AddTest1JS();
}
void it_should_contain_only_one_file()
{
bundle.GroupBundles["default"].Assets.Count.Is(1);
}
}
class then_I_add_the_same_file : then_I_add_a_file
{
void it_should_still_contain_only_one_file()
{
bundle.GroupBundles["default"].Assets.Count.Is(1);
}
}
}How can I improve this or am I already on the right track?
Solution
Here is how I've written most of my NSpec tests, I usually start a context out with an act. This dry's up the specification. If I find myself doing too many "acts", I end up breaking it up into separate specifications.
class describe_JavaScriptBundle : nspec
{
JavaScriptBundle bundle;
void AddTest1JS()
{
bundle.Add("test.js");
}
void before_each()
{
bundle = Bundle.JavaScript();
}
void adding_a_bundle()
{
context["no files added"] = () =>
{
it["GroupBundle contains default key"] = () =>
bundle.GroupBundles.ContainsKey("default").is_true();
it["has no Assets"] () =>
bundle.GroupBundles["default"].Assets.Count.Is(0);
};
context["adding one file"] = () =>
{
act = () => AddTest1JS();
it["contains the file"] = () =>
bundle.GroupBundles["default"].Assets.Count.Is(1);
context["adding the same file again"] = () =>
{
act = () => AddTest1JS();
it["should still only contain one file"] = () =>
bundle.GroupBundles["default"].Assets.Count.Is(1);
};
};
}
}Code Snippets
class describe_JavaScriptBundle : nspec
{
JavaScriptBundle bundle;
void AddTest1JS()
{
bundle.Add("test.js");
}
void before_each()
{
bundle = Bundle.JavaScript();
}
void adding_a_bundle()
{
context["no files added"] = () =>
{
it["GroupBundle contains default key"] = () =>
bundle.GroupBundles.ContainsKey("default").is_true();
it["has no Assets"] () =>
bundle.GroupBundles["default"].Assets.Count.Is(0);
};
context["adding one file"] = () =>
{
act = () => AddTest1JS();
it["contains the file"] = () =>
bundle.GroupBundles["default"].Assets.Count.Is(1);
context["adding the same file again"] = () =>
{
act = () => AddTest1JS();
it["should still only contain one file"] = () =>
bundle.GroupBundles["default"].Assets.Count.Is(1);
};
};
}
}Context
StackExchange Code Review Q#3712, answer score: 4
Revisions (0)
No revisions yet.