public
Last active — forked from benfoster/gist:3845201

NSpec Example 2

  • Download Gist
gistfile1.cs
C#
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
class describe_Accounts : nspec
{
Account account;
 
void before_each()
{
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();
}
 
void adding_a_site
{
context["when the site already exists"] = () =>
{
before = () => account = GetAccountWithSite();
act = () => account.AddSite("sites/1");
 
it["throws excepton"] = expect<InvalidOperationException>();
};
 
context["when no subscriptions exist"] = () =>
{
before = () => account.AddSite("sites/1");
 
it["doesn't add the site"] = () =>
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"] = () =>
account.Sites.Count.ShouldEqual(1);
}
 
context["when available subscriptions exists"] = () =>
{
before = () =>
account.AddSubscription(GetValidSubscription());
 
act = () => account.AddSite("sites/1");
 
it["adds the site"] = () =>
account.Sites.Count.ShouldEqual(1);
};
}
 
bool result;
void 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");
 
it["is valid"] = () =>
result.should_be_false();
};
 
context["when the specified site is not mapped to the account
{
act = () =>
result = account.ValidateSite("sites/2");
 
it["is invalid"] = () =>
result.should_be_false();
}
}
 
void 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());
 
it["returns true"] = () =>
result.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));
 
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;
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.