Skip to content

Embed URL

HTTPS clone URL

Subversion checkout URL

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