by Patrik Hägne via Legend and truth on 6/20/2010 1:28:00 PM
This is the sixth 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.
This is a final test to make sure the code handles the case of an account not getting found. This is not too hard to write:
[Test] public void Should_throw_when_account_does_not_exist() { // Arrange A.CallTo(() => this.accountRepository.Find(A<string>.Ignored)).ReturnsNull(); // Act, Assert Assert.Throws<AccountNotFoundException>(() => this.service.Login("username", "password")); }
This test takes advantage of the fact that later configurations of a fake object takes precedence over earlier configurations, this differs from the way that Rhino Mocks works where earlier configurations has precedence.
A login is attempted which should fail.
To get this test to compile, you'll need to add a new exception class:AccountNotFoundException.
namespace FakeItEasy.Examples.LoginService { using System; public class AccountNotFoundException : Exception { } }
The test currently fails with a null reference exception, a quick fix is needed at the top of the method.
namespace FakeItEasy.Examples.LoginService { using System; public class LoginService { private IAccountRepository accountRepository; private int numberOfFailedAttempts; private string previousUsername; public LoginService(IAccountRepository accountRepository) { this.accountRepository = accountRepository; this.previousUsername = string.Empty; } public void Login(string username, string password) { var account = this.accountRepository.Find(username); if (account == null) { throw new AccountNotFoundException(); } 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); } } } }
Original Post: FakeItEasy Login Service Example Series – Part 6
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.