Skip to content

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Fun with tests.
namespace Accounts
{
namespace NewAccounts
{
public class When_creating_a_new_account {
Account account = new Account();
public void Does_not_have_any_subscriptions() {
account.Subscriptions.Count.ShouldEqual(0);
}
public void Does_not_have_any_sites() {
account.Subscriptions.Count.ShouldEqual(0);
}
public void Generates_a_new_api_key() {
account.ApiKeys.First().ShouldNotBeEmpty();
}
}
}
namespace adding_a_site
{
/* This is a bad test. It's coupled to the implementation of the object mother's factory method */
public class When_the_site_already_exists
{
public void ThrowsException() {
Account account = ObjectMother.GetAccountWithSite();
typeof(InvalidOperationException).ShouldBeThrownBy(() => account.AddSite("sites/1"));
}
}
/* Collapsed before/act etc. It added no value. */
public class When_no_subscriptions_exist {
public void Should_not_add_the_site() {
Account account = new Account();
account.AddSite("sites/1");
account.Sites.Count.ShouldEqual(0);
}
}
/* see above. there's no need to split out AAA */
public class When_no_available_subscriptions_exist {
public void Should_not_add_the_site() {
Account account = GetAccountWithSite();
account.AddSite("sites/2");
account.Sites.Count.ShouldEqual(1);
}
}
public class When_available_subscriptions_exist {
public void Should_add_the_site() {
Account account = new Account();
account.AddSubscription(ObjectMother.GetValidSubscription());
account.AddSite("sites/1");
account.Sites.Count.ShouldEqual(1);
}
}
/* These previous tests should really be consolidated into one test class
Separating out into indvidual classes doesn't add any value.
*/
}
/* Also don't like this test. Change GetAccountWithSite with a different site, BOOM */
public class validating_sites
{
Account account = ObjectMother.GetAccountWithSite();
public void When_the_specified_site_is_mapped_to_the_account_should_be_valid {
account.ValidateSite("sites/1").ShouldBeTrue();
}
public void When_the_specified_site_is_not_mapped_to_the_account() {
account.ValidateSite("sites/2").ShouldBeFalse();
}
}
/* Common act, also a bad idea. It adds nothing but needless indirection */
public class validating_subscriptions
{
public void When_the_account_has_a_valid_subscription_should_report_as_valid {
Account account = new Account();
account.AddSubscription(GetValidSubscription());
account.HasValidSubscription().ShouldBeTrue();
}
public void When_the_account_has_no_valid_subscriptions_should_report_as_invalid {
Account account = new Account();
account.AddSite("sites/1");
var subscription = new Subscription(
"products/1",
"Fabrik Subscription",
69M,
DateTime.UtcNow.AddMonths(-13), // expired by one month
new SubscriptionDuration(1, SubscriptionPeriod.Yearly));
account.AddSubscription(subscription);
account.HasValidSubscription().ShouldBeFalse();
}
}
public static class ObjectMother {
public Subscription GetValidSubscription()
{
return new Subscription(
"products/1",
"Fabrik Subscription",
69M,
DateTime.UtcNow,
new SubscriptionDuration(1, SubscriptionPeriod.Yearly)
);
}
public Account GetAccountWithSite()
{
var account = new Account();
account.AddSubscription(GetValidSubscription());
account.AddSite("sites/1");
return account;
}
}
}
@jbogard
Owner

Whew! Got rid of all the common arrange/acts as these tests really didn't need them. Only one test might need test class per fixture pattern (the first). The rest really needed to have their own isolated fixtures.

I'm not a huge fan of ObjectMother, I'd rather use AutoFixture in this case, or Fubu's way of object builder specifications for complex domains.

@jbogard
Owner

One other thing - when a test fails here, I can use standard IDE tools to navigate to type/member. With strings as keys representing test methods, I'm relegated to grep tools (not in my IDE) or "Find in Files" (much, much clunkier).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.