by Patrik Hägne via Legend and truth on 7/24/2010 11:18:00 AM
This is the seventh and last part in the series of posts where I’m porting Brett Schucherts excelent demo of Mockito in Java to C# and FakeItEasy.
The source for this example series can be found in a Mercurial repository at Google code. Each test implementation and following code update is a separate commit so you can easily update your repository to look at the full code at any given state. Find the repository here.
Part 1 can be found here.
Part 2 can be found here.
Part 3 can be found here.
Part 4 can be found here.
Part 5 can be found here.
Part 6 can be found here.
The next test is similar to the previous test. A revoked account does not allow logins:
[Test] public void Should_throw_when_account_has_been_revoked() { // Arrange A.CallTo(() => this.account.IsRevoked).Returns(true); // Act, Assert Assert.Throws<AccountRevokedException>(() => this.service.Login("username", "password")); }
This test is a repeat of the previous test, checking for a different result from a different starting condition.
You'll need to add another exception, AccountRevokedException and a new property, IsRevoked, to IAccount.
The only update to get to green is adding a check - a guard clause - similar to the previous test:
public void Login(string username, string password) { var account = this.accountRepository.Find(username); if (account == null) { throw new AccountNotFoundException(); } if (account.IsRevoked) { throw new AccountRevokedException(); } if (account.IsLoggedIn) { throw new AccountLoginLimitReachedException(); } if (account.PasswordMatches(password)) { account.SetLoggedIn(true); } else { if (this.previousUsername.Equals(username)) { this.numberOfFailedAttempts++; } else { this.numberOfFailedAttempts = 1; this.previousUsername = username; } } if (this.numberOfFailedAttempts == 3) { account.SetRevoked(true); } }
This has been a port of Brett’s original tutorial, his tutorial does not end here though no more tests are added. Please continue reading his tutorial.
Original Post: FakeItEasy Login Service Example Series – Part 7
The content of the postings is owned by the respective author. CSharpFeeds is not responsible for the contents of the postings. This site is automatically generated and cannot be reviewed for abusive content. If you find abusive content on CSharpFeeds, please contact us. Designated trademarks and brands are the property of their respective owners. All rights reserved.