by Patrik Hägne via Legend and truth on 5/23/2010 6:21: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.
The first two tests have made good progress, however to keep the number of assertions per test small (so far one) and to make individual tests less dependent on the underlying implementation, this next test forces a fix to the code and probably would have been a better second test than one you just created.
[Test] public void Should_not_log_in_when_password_is_wrong() { // Arrange A.CallTo(() => this.account.PasswordMatches(A<string>.Ignored)).Returns(false); // Act this.service.Login("username", "wrong password"); // Assert A.CallTo(() => this.account.SetLoggedIn(true)).MustNotHaveHappened(); }
This test takes advantage of the recent test refactoring. Before ever getting into the test method, the SetUp method:
There’s not much left:
This test did not require any existing classes to have new methods added.
The test fails with this message, FakeItEasy finds the specified method call once and lists all the calls to the faked IAccount.
Assertion failed for the following call: 'FakeItEasy.Examples.LoginService.IAccount.SetLoggedIn(True)' Expected to find it exactly never but found it #1 times among the calls: 1. 'FakeItEasy.Examples.LoginService.IAccount.SetLoggedIn(True)' 2. 'FakeItEasy.Examples.LoginService.IAccount.PasswordMatches("wrong password")'
The LoginService needs to be updated:
namespace FakeItEasy.Examples.LoginService { using System; public class LoginService { private IAccountRepository accountRepository; private int numberOfFailedAttempts; public LoginService(IAccountRepository accountRepository) { this.accountRepository = accountRepository; } public void Login(string username, string password) { var account = this.accountRepository.Find(username); if (account.PasswordMatches(password)) { account.SetLoggedIn(true); } else { this.numberOfFailedAttempts++; } if (this.numberOfFailedAttempts == 3) { account.SetRevoked(true); } } } }
Original Post: FakeItEasy Login Service Example Series – Part 3
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.