Skip to content

Instantly share code, notes, and snippets.

@jbogard
Last active December 23, 2015 20:39
Show Gist options
  • Select an option

  • Save jbogard/6690905 to your computer and use it in GitHub Desktop.

Select an option

Save jbogard/6690905 to your computer and use it in GitHub Desktop.

Revisions

  1. jbogard revised this gist Sep 25, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion gistfile1.cs
    Original file line number Diff line number Diff line change
    @@ -31,7 +31,7 @@ public void ThrowsException() {
    }
    }

    /* Collapsed before/after etc. It added no value. */
    /* Collapsed before/act etc. It added no value. */
    public class When_no_subscriptions_exist {

    public void Should_not_add_the_site() {
  2. jbogard revised this gist Sep 25, 2013. 1 changed file with 89 additions and 88 deletions.
    177 changes: 89 additions & 88 deletions gistfile1.cs
    Original file line number Diff line number Diff line change
    @@ -18,122 +18,123 @@ public void Generates_a_new_api_key() {
    }
    }
    }
    /*

    namespace adding_a_site
    {
    context["when the site already exists"] = () =>
    /* 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
    {
    before = () => account = GetAccountWithSite();
    act = () => account.AddSite("sites/1");
    it["throws excepton"] = expect<InvalidOperationException>();
    };
    public void ThrowsException() {
    Account account = ObjectMother.GetAccountWithSite();
    typeof(InvalidOperationException).ShouldBeThrownBy(() => account.AddSite("sites/1"));
    }
    }

    context["when no subscriptions exist"] = () =>
    {
    before = () => account.AddSite("sites/1");
    /* Collapsed before/after etc. It added no value. */
    public class When_no_subscriptions_exist {

    it["doesn't add the site"] = () =>
    public void Should_not_add_the_site() {
    Account account = new Account();

    account.AddSite("sites/1");

    account.Sites.Count.ShouldEqual(0);
    };
    context["when no available subscriptiosn exist"] = () =>
    {
    before = () => account = GetAccountWithSite();
    }

    act = () => account.AddSite("sites/2");
    it["doesn't add the site"] = () =>
    }

    /* 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 {

    context["when available subscriptions exists"] = () =>
    {
    before = () =>
    account.AddSubscription(GetValidSubscription());
    act = () => account.AddSite("sites/1");
    it["adds the site"] = () =>
    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.
    */
    }

    bool result;
    void validating_sites()
    /* Also don't like this test. Change GetAccountWithSite with a different site, BOOM */
    public class validating_sites
    {
    before = () => account = GetAccountWithSite(); //common before move it up a context (shouldnt hurt redability)?
    context["when the specified site is mapped to the account"] = () =>
    {
    act = () =>
    result = account.ValidateSite("sites/1");
    Account account = ObjectMother.GetAccountWithSite();

    it["is valid"] = () =>
    result.should_be_false();
    };
    context["when the specified site is not mapped to the account
    {
    act = () =>
    result = account.ValidateSite("sites/2");
    public void When_the_specified_site_is_mapped_to_the_account_should_be_valid {

    account.ValidateSite("sites/1").ShouldBeTrue();
    }

    it["is invalid"] = () =>
    result.should_be_false();
    public void When_the_specified_site_is_not_mapped_to_the_account() {

    account.ValidateSite("sites/2").ShouldBeFalse();
    }
    }

    void validating_subscriptions()
    /* Common act, also a bad idea. It adds nothing but needless indirection */
    public class validating_subscriptions
    {
    act = () => result = account.HasValidSubscription(); //common act, move it up a context (shouldnt hurt redability)?
    context["when the account has a valid subscription"] = () =>
    {
    before = () => account.AddSubscription(GetValidSubscription());
    public void When_the_account_has_a_valid_subscription_should_report_as_valid {
    Account account = new Account();

    account.AddSubscription(GetValidSubscription());

    it["returns true"] = () =>
    result.ShouldBeTrue();
    };
    account.HasValidSubscription().ShouldBeTrue();
    }

    context["when the account has no valid subscriptions"] = () =>
    {
    before = () =>
    {
    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));
    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.AddSubscription(subscription);

    it["returns false"] = () =>
    result.ShouldBeFalse();
    account.HasValidSubscription().ShouldBeFalse();
    }
    }

    Subscription GetValidSubscription()
    {
    return new Subscription(
    "products/1",
    "Fabrik Subscription",
    69M,
    DateTime.UtcNow,
    new SubscriptionDuration(1, SubscriptionPeriod.Yearly)
    );
    }
    public static class ObjectMother {
    public Subscription GetValidSubscription()
    {
    return new Subscription(
    "products/1",
    "Fabrik Subscription",
    69M,
    DateTime.UtcNow,
    new SubscriptionDuration(1, SubscriptionPeriod.Yearly)
    );
    }

    Account GetAccountWithSite()
    {
    var account = new Account();
    account.AddSubscription(GetValidSubscription());
    account.AddSite("sites/1");
    return account;
    public Account GetAccountWithSite()
    {
    var account = new Account();
    account.AddSubscription(GetValidSubscription());
    account.AddSite("sites/1");
    return account;
    }
    }

    */
    }
  3. jbogard revised this gist Sep 25, 2013. 1 changed file with 7 additions and 5 deletions.
    12 changes: 7 additions & 5 deletions gistfile1.cs
    Original file line number Diff line number Diff line change
    @@ -2,23 +2,23 @@ namespace Accounts
    {
    namespace NewAccounts
    {
    class When_creating_a_new_account {
    public class When_creating_a_new_account {
    Account account = new Account();

    void Does_not_have_any_subscriptions() {
    public void Does_not_have_any_subscriptions() {
    account.Subscriptions.Count.ShouldEqual(0);
    }

    void Does_not_have_any_sites() {
    public void Does_not_have_any_sites() {
    account.Subscriptions.Count.ShouldEqual(0);
    }

    void Generates_a_new_api_key() {
    public void Generates_a_new_api_key() {
    account.ApiKeys.First().ShouldNotBeEmpty();
    }
    }
    }

    /*
    namespace adding_a_site
    {
    context["when the site already exists"] = () =>
    @@ -134,4 +134,6 @@ Account GetAccountWithSite()
    account.AddSite("sites/1");
    return account;
    }
    */
    }
  4. jbogard revised this gist Sep 24, 2013. 1 changed file with 18 additions and 18 deletions.
    36 changes: 18 additions & 18 deletions gistfile1.cs
    Original file line number Diff line number Diff line change
    @@ -1,25 +1,25 @@
    class describe_Accounts : nspec
    namespace Accounts
    {
    Account account;

    void before_each()
    namespace NewAccounts
    {
    account = new Account(); //agree that moving this up doesn't hurt readability?
    }

    void when_creating_a_new_account()
    {
    it["doesn't have any subscriptions"] = () =>
    account.Subscriptions.Count.ShouldEqual(0);

    it["doesn't have any sites"] = () =>
    account.Sites.Count.ShouldEqual(0);

    it["generates a new api key"] = () =>
    account.ApiKeys.First().ShouldNotBeEmpty();
    class When_creating_a_new_account {
    Account account = new Account();

    void Does_not_have_any_subscriptions() {
    account.Subscriptions.Count.ShouldEqual(0);
    }

    void Does_not_have_any_sites() {
    account.Subscriptions.Count.ShouldEqual(0);
    }

    void Generates_a_new_api_key() {
    account.ApiKeys.First().ShouldNotBeEmpty();
    }
    }
    }

    void adding_a_site
    namespace adding_a_site
    {
    context["when the site already exists"] = () =>
    {
  5. @amirrajan amirrajan revised this gist Oct 6, 2012. 1 changed file with 106 additions and 157 deletions.
    263 changes: 106 additions & 157 deletions gistfile1.cs
    Original file line number Diff line number Diff line change
    @@ -1,188 +1,137 @@
    [Subject(typeof(Account))]
    public class AccountSpecs
    {
    static Account account;
    class describe_Accounts : nspec
    {
    Account account;

    public class When_creating_a_new_account
    {
    Because of = ()
    => account = new Account();
    void before_each()
    {
    account = new Account(); //agree that moving this up doesn't hurt readability?
    }

    It Should_not_have_any_subscription = ()
    => account.Subscriptions.Count.ShouldEqual(0);
    void when_creating_a_new_account()
    {
    it["doesn't have any subscriptions"] = () =>
    account.Subscriptions.Count.ShouldEqual(0);

    It Should_not_have_any_sites = ()
    => account.Sites.Count.ShouldEqual(0);
    it["doesn't have any sites"] = () =>
    account.Sites.Count.ShouldEqual(0);

    It Should_generate_a_new_api_key = ()
    => account.ApiKeys.First().ShouldNotBeEmpty();
    }
    it["generates a new api key"] = () =>
    account.ApiKeys.First().ShouldNotBeEmpty();
    }

    [Subject(typeof(Account), "Adding sites")]
    public class Adding_a_site
    void adding_a_site
    {
    context["when the site already exists"] = () =>
    {
    static Exception exception;

    public class When_the_site_already_exists
    {
    Establish ctx = ()
    => account = GetAccountWithSite();

    Because of = ()
    => exception = Catch.Exception(() => account.AddSite("sites/1"));

    It Should_throw = ()
    => exception.ShouldBeOfType<InvalidOperationException>();
    }

    public class When_no_subscriptions_exist
    {
    Establish ctx = ()
    => account = new Account();

    Because of = ()
    => account.AddSite("sites/1");

    It Should_not_add_the_site = ()
    => account.Sites.Count.ShouldEqual(0);
    }

    public class When_no_available_subscriptions_exist
    {
    Establish ctx = ()
    => account = GetAccountWithSite();
    before = () => account = GetAccountWithSite();

    act = () => account.AddSite("sites/1");

    Because of = ()
    => account.AddSite("sites/2");
    it["throws excepton"] = expect<InvalidOperationException>();
    };

    It Should_not_add_the_site = ()
    => account.Sites.Count.ShouldEqual(1);
    }
    context["when no subscriptions exist"] = () =>
    {
    before = () => account.AddSite("sites/1");

    public class When_an_available_subscription_exists
    {
    Establish ctx = () =>
    {
    account = new Account();
    account.AddSubscription(GetValidSubscription());
    };

    Because of = ()
    => account.AddSite("sites/1");

    It Should_add_the_site = ()
    => account.Sites.Count.ShouldEqual(1);
    }
    }
    it["doesn't add the site"] = () =>
    account.Sites.Count.ShouldEqual(0);
    };

    [Subject(typeof(Account), "Validating sites")]
    public class Validating_sites
    context["when no available subscriptiosn exist"] = () =>
    {
    static bool result;
    before = () => account = GetAccountWithSite();

    public class When_the_specified_site_is_mapped_to_the_account
    {
    Establish ctx = ()
    => account = GetAccountWithSite();

    Because of = ()
    => result = account.ValidateSite("sites/1");
    act = () => account.AddSite("sites/2");

    It Should_return_true = ()
    => result.ShouldBeTrue();
    }

    public class When_the_specified_site_is_not_mapped_to_the_account
    {
    Establish ctx = ()
    => account = GetAccountWithSite();

    Because of = ()
    => result = account.ValidateSite("sites/2");

    It Should_return_false = ()
    => result.ShouldBeFalse();
    }
    it["doesn't add the site"] = () =>
    account.Sites.Count.ShouldEqual(1);
    }

    [Subject(typeof(Account), "Validating subscriptions")]
    public class Validating_subscriptions
    context["when available subscriptions exists"] = () =>
    {
    static bool result;

    public class When_the_account_has_a_valid_subscription
    {
    Establish ctx = () =>
    {
    account = new Account();
    account.AddSubscription(GetValidSubscription());
    };
    before = () =>
    account.AddSubscription(GetValidSubscription());

    Because of = ()
    => result = account.HasValidSubscription();
    act = () => account.AddSite("sites/1");

    It Should_return_true = ()
    => result.ShouldBeTrue();
    }
    it["adds the site"] = () =>
    account.Sites.Count.ShouldEqual(1);
    };
    }

    public class When_the_account_has_no_valid_subscriptions
    {
    Establish ctx = () =>
    {
    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);
    };

    Because of = ()
    => result = account.HasValidSubscription();

    It Should_return_false = ()
    => result.ShouldBeFalse();
    }
    }
    bool result;
    void validating_sites()
    {
    before = () => account = GetAccountWithSite(); //common before move it up a context (shouldnt hurt redability)?

    public class When_resetting_api_key
    context["when the specified site is mapped to the account"] = () =>
    {
    static string currentKey;
    act = () =>
    result = account.ValidateSite("sites/1");

    Establish ctx = () =>
    {
    account = new Account();
    currentKey = account.ApiKeys.First();
    };
    it["is valid"] = () =>
    result.should_be_false();
    };

    Because of = ()
    => account.ResetApiKey();
    context["when the specified site is not mapped to the account
    {
    act = () =>
    result = account.ValidateSite("sites/2");

    It Should_generate_a_new_key = ()
    => currentKey.ShouldNotEqual(account.ApiKeys.First());
    it["is invalid"] = () =>
    result.should_be_false();
    }
    }

    void validating_subscriptions()
    {
    act = () => result = account.HasValidSubscription(); //common act, move it up a context (shouldnt hurt redability)?

    private static Subscription GetValidSubscription()
    context["when the account has a valid subscription"] = () =>
    {
    return new Subscription(
    "products/1",
    "Fabrik Subscription",
    69M,
    DateTime.UtcNow,
    new SubscriptionDuration(1, SubscriptionPeriod.Yearly)
    );
    }
    before = () => account.AddSubscription(GetValidSubscription());

    it["returns true"] = () =>
    result.ShouldBeTrue();
    };

    private static Account GetAccountWithSite()
    context["when the account has no valid subscriptions"] = () =>
    {
    var account = new Account();
    account.AddSubscription(GetValidSubscription());
    account.AddSite("sites/1");
    return account;
    before = () =>
    {
    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);
    };

    it["returns false"] = () =>
    result.ShouldBeFalse();
    }
    }

    }
    Subscription GetValidSubscription()
    {
    return new Subscription(
    "products/1",
    "Fabrik Subscription",
    69M,
    DateTime.UtcNow,
    new SubscriptionDuration(1, SubscriptionPeriod.Yearly)
    );
    }

    Account GetAccountWithSite()
    {
    var account = new Account();
    account.AddSubscription(GetValidSubscription());
    account.AddSite("sites/1");
    return account;
    }
    }
  6. @benfoster benfoster created this gist Oct 6, 2012.
    188 changes: 188 additions & 0 deletions gistfile1.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,188 @@
    [Subject(typeof(Account))]
    public class AccountSpecs
    {
    static Account account;

    public class When_creating_a_new_account
    {
    Because of = ()
    => account = new Account();

    It Should_not_have_any_subscription = ()
    => account.Subscriptions.Count.ShouldEqual(0);

    It Should_not_have_any_sites = ()
    => account.Sites.Count.ShouldEqual(0);

    It Should_generate_a_new_api_key = ()
    => account.ApiKeys.First().ShouldNotBeEmpty();
    }

    [Subject(typeof(Account), "Adding sites")]
    public class Adding_a_site
    {
    static Exception exception;

    public class When_the_site_already_exists
    {
    Establish ctx = ()
    => account = GetAccountWithSite();

    Because of = ()
    => exception = Catch.Exception(() => account.AddSite("sites/1"));

    It Should_throw = ()
    => exception.ShouldBeOfType<InvalidOperationException>();
    }

    public class When_no_subscriptions_exist
    {
    Establish ctx = ()
    => account = new Account();

    Because of = ()
    => account.AddSite("sites/1");

    It Should_not_add_the_site = ()
    => account.Sites.Count.ShouldEqual(0);
    }

    public class When_no_available_subscriptions_exist
    {
    Establish ctx = ()
    => account = GetAccountWithSite();

    Because of = ()
    => account.AddSite("sites/2");

    It Should_not_add_the_site = ()
    => account.Sites.Count.ShouldEqual(1);
    }

    public class When_an_available_subscription_exists
    {
    Establish ctx = () =>
    {
    account = new Account();
    account.AddSubscription(GetValidSubscription());
    };

    Because of = ()
    => account.AddSite("sites/1");

    It Should_add_the_site = ()
    => account.Sites.Count.ShouldEqual(1);
    }
    }

    [Subject(typeof(Account), "Validating sites")]
    public class Validating_sites
    {
    static bool result;

    public class When_the_specified_site_is_mapped_to_the_account
    {
    Establish ctx = ()
    => account = GetAccountWithSite();

    Because of = ()
    => result = account.ValidateSite("sites/1");

    It Should_return_true = ()
    => result.ShouldBeTrue();
    }

    public class When_the_specified_site_is_not_mapped_to_the_account
    {
    Establish ctx = ()
    => account = GetAccountWithSite();

    Because of = ()
    => result = account.ValidateSite("sites/2");

    It Should_return_false = ()
    => result.ShouldBeFalse();
    }
    }

    [Subject(typeof(Account), "Validating subscriptions")]
    public class Validating_subscriptions
    {
    static bool result;

    public class When_the_account_has_a_valid_subscription
    {
    Establish ctx = () =>
    {
    account = new Account();
    account.AddSubscription(GetValidSubscription());
    };

    Because of = ()
    => result = account.HasValidSubscription();

    It Should_return_true = ()
    => result.ShouldBeTrue();
    }

    public class When_the_account_has_no_valid_subscriptions
    {
    Establish ctx = () =>
    {
    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);
    };

    Because of = ()
    => result = account.HasValidSubscription();

    It Should_return_false = ()
    => result.ShouldBeFalse();
    }
    }

    public class When_resetting_api_key
    {
    static string currentKey;

    Establish ctx = () =>
    {
    account = new Account();
    currentKey = account.ApiKeys.First();
    };

    Because of = ()
    => account.ResetApiKey();

    It Should_generate_a_new_key = ()
    => currentKey.ShouldNotEqual(account.ApiKeys.First());
    }

    private static Subscription GetValidSubscription()
    {
    return new Subscription(
    "products/1",
    "Fabrik Subscription",
    69M,
    DateTime.UtcNow,
    new SubscriptionDuration(1, SubscriptionPeriod.Yearly)
    );
    }

    private static Account GetAccountWithSite()
    {
    var account = new Account();
    account.AddSubscription(GetValidSubscription());
    account.AddSite("sites/1");
    return account;
    }

    }