public
Last active

MSpec 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 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 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;
}
 
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.