Last active
February 25, 2016 14:05
-
-
Save adamralph/5535794 to your computer and use it in GitHub Desktop.
xBehave example for http://stackoverflow.com/a/5586525/49241
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Linq; | |
using FluentAssertions; | |
using Xbehave; | |
using Xunit; | |
public class AccountsFeature | |
{ | |
private Account account; | |
[Background] | |
public void Background() | |
{ | |
"Given a new account" | |
.Given(() => account = new Account()); | |
} | |
[Scenario] | |
public void CreatingANewAccount() | |
{ | |
"Then it doesn't have any subscriptions" | |
.Then(() => account.Subscriptions.Count.Should().Be(0)); | |
"And it doesn't have any sites" | |
.And(() => account.Sites.Count.Should().Be(0)); | |
"And it generates a new api key" | |
.And(() => account.ApiKeys.FirstOrDefault().Should().NotBeNull()); | |
} | |
[Scenario] | |
public void AddingAnExistingSite(Exception ex) | |
{ | |
"Given the account has a site" | |
.Given(() => account = Helper.WithSite(account)); | |
"When I add a site" | |
.When(() => ex = Record.Exception(() => account.AddSite("sites/1"))); | |
"Then an invalid operation exception is thrown" | |
.Then(() => ex.Should().BeOfType<InvalidOperationException>()); | |
} | |
[Scenario] | |
public void NoSubscriptionsExist() | |
{ | |
"When I add a site" | |
.When(() => account.AddSite("sites/1")); | |
"Then the site is not added" | |
.Then(() => account.Sites.Count.Should().Be(0)); | |
} | |
[Scenario] | |
public void NoAvailableSubscriptionsExist() | |
{ | |
"Given the account has a site" | |
.Given(() => account = Helper.WithSite(account)); | |
"When I add another site" | |
.When(() => account.AddSite("sites/1")); | |
"Then the site is not added" | |
.Then(() => account.Sites.Count.Should().Be(1)); | |
} | |
[Scenario] | |
public void AvailableSubscriptionsExist() | |
{ | |
"Given the account has a valid subscription" | |
.Given(() => account.AddSubscription(Helper.GetValidSubscription())); | |
"When I add another site" | |
.When(() => account.AddSite("sites/1")); | |
"Then the site is added" | |
.Then(() => account.Sites.Count.Should().Be(1)); | |
} | |
} | |
public class SiteValidationFeature | |
{ | |
private Account account; | |
[Background] | |
public void Background() | |
{ | |
"Given a new account with a site" | |
.Given(() => account = Helper.WithSite(new Account())); | |
} | |
[Scenario] | |
public void TheSpecifiedSiteIsMappedToTheAccount(bool result) | |
{ | |
"When I validate the site" | |
.When(() => result = account.ValidateSite("sites/1")); | |
"Then the site is valid" | |
.Then(() => result.Should().BeTrue()); | |
} | |
[Scenario] | |
public void TheSpecifiedSiteIsNotMappedToTheAccount(bool result) | |
{ | |
"When I validate the site" | |
.When(() => result = account.ValidateSite("sites/2")); | |
"Then the site is invalid" | |
.Then(() => result.Should().BeFalse()); | |
} | |
} | |
public class SubscriptionValidationFeature | |
{ | |
[Scenario] | |
public void TheAccountHasAValidSubscription(Account account, bool result) | |
{ | |
"Given an account with a valid subscription" | |
.Given(() => | |
{ | |
account = new Account(); | |
account.AddSubscription(Helper.GetValidSubscription()); | |
}); | |
"When checking that the account has a valid subscription" | |
.When(() => result = account.HasValidSubscription()); | |
"Then the account is confirmed to have a valid subscription" | |
.Then(() => result.Should().BeTrue()); | |
} | |
[Scenario] | |
public void TheAccountHasNoValidSubscriptions(Account account, bool result) | |
{ | |
"Given an account with a valid subscription" | |
.Given(() => | |
{ | |
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); | |
}); | |
"When checking that the account has a valid subscription" | |
.When(() => result = account.HasValidSubscription()); | |
"Then the account is found to have no valid subscriptions" | |
.Then(() => result.Should().BeFalse()); | |
} | |
} | |
public static class Helper | |
{ | |
public static Subscription GetValidSubscription() | |
{ | |
return new Subscription( | |
"products/1", | |
"Fabrik Subscription", | |
69M, | |
DateTime.UtcNow, | |
new SubscriptionDuration(1, SubscriptionPeriod.Yearly) | |
); | |
} | |
public static Account WithSite(Account account) | |
{ | |
account.AddSubscription(Helper.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