Skip to content

Instantly share code, notes, and snippets.

@jbogard
Last active December 23, 2015 20:39
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jbogard/6690905 to your computer and use it in GitHub Desktop.
Save jbogard/6690905 to your computer and use it in GitHub Desktop.
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
Copy link
Author

jbogard commented Sep 25, 2013

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