by Patrik Hägne via Legend and truth on 5/25/2010 8:42:00 PM
This is the fourth 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.
This is one of those requirements you ask "Really?!" This requirement comes from an actual project, so while it might sound bogus, it is an actual requirement from the real world.
[Test] public void Should_not_revoke_other_account_when_first_account_has_failed_to_log_in_twice() { // Arrange A.CallTo(() => this.account.PasswordMatches(A<string>.Ignored)).Returns(false); var secondAccount = A.Fake<IAccount>(); A.CallTo(() => secondAccount.PasswordMatches(A<string>.Ignored)).Returns(false); A.CallTo(() => this.accountRepository.Find("other username")).Returns(secondAccount); // Act this.service.Login("username", "wrong password"); this.service.Login("username", "wrong password"); this.service.Login("other username", "wrong password"); // Assert A.CallTo(() => secondAccount.SetRevoked(true)).MustNotHaveHappened(); }
This test is a little longer because it requires more setup. Rather than possibly messing up existing tests and adding more setup to the fixture, I decided to do it in this test. There are alternatives to writing this test's setup:
Since my primary purpose of this tutorial is to demo FakeItEasy, I'll leave it as is until I notice additional duplication.
There are 4 parts to this test:
This test compiles without any new methods. It does fail with the following exception:
Assertion failed for the following call: 'FakeItEasy.Examples.LoginService.IAccount.SetRevoked(True)' Expected to find it exactly never but found it #1 times among the calls: 1. 'FakeItEasy.Examples.LoginService.IAccount.PasswordMatches("wrong password")' 2. 'FakeItEasy.Examples.LoginService.IAccount.SetRevoked(True)'
To get this new test to pass, I added a new member to the LoginService class: previousUsername. Then I updated the login method to take advantage of it:
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.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 allows all tests to pass. Would it have been possible to do less? Maybe, but this was the first thing that came to mind. The code is starting to be a bit unruly. We're just about ready to clean up this code, but before we do there are a few more tests.
Original Post: FakeItEasy Login Service Example Series – Part 4
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.