Skip to content

Instantly share code, notes, and snippets.

@laribee
Created October 3, 2017 17:38
Show Gist options
  • Save laribee/8cb5d0820fbcee489480f75596db45a6 to your computer and use it in GitHub Desktop.
Save laribee/8cb5d0820fbcee489480f75596db45a6 to your computer and use it in GitHub Desktop.
using System;
using NSubstitute;
using Xunit;
namespace tdd_mvvm
{
public class LoginPresenterTests
{
[Fact]
public void SubscribeToViewsLoginSubmit()
{
var view = Substitute.For<IPresenterView>();
var subject = new LoginPresenter(view, null);
view.Received().SubmitLogin += subject.OnLoginSubmitted;
}
[Fact]
public void WhenLoginSubmitIsTriggeredGrabUsernameAndPassword()
{
var view = Substitute.For<IPresenterView>();
var subject = new LoginPresenter(view, null);
view.SubmitLogin += Raise.EventWith(new object(), new EventArgs());
var assertUsername = view.Received().Username;
var assertPassword = view.Received().Password;
}
[Fact]
public void ForwardUsernameAndPasswordToTheAuthenticationServie()
{
var view = Substitute.For<IPresenterView>();
var authService = Substitute.For<IAuthenticationService>();
var subject = new LoginPresenter(view, authService);
view.SubmitLogin += Raise.EventWith(new object(), new EventArgs());
authService.Received().Authenticate(Arg.Any<string>, Arg.Any<string>);
}
}
public interface IAuthenticationService
{
void Authenticate(string username, string password);
}
public interface IPresenterView
{
event EventHandler SubmitLogin;
string Username { get; }
string Password { get; set; }
}
public class LoginPresenter
{
private readonly IPresenterView _view;
public LoginPresenter(IPresenterView view, IAuthenticationService authService)
{
_view = view;
_view.SubmitLogin += OnLoginSubmitted;
}
public void OnLoginSubmitted(object source, EventArgs args)
{
var username = _view.Username;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment