Skip to content

@benfoster /gist:3845201
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
MSpec Example 2
[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;
}
}
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.