daily pastebin goal
7%
SHARE
TWEET

Untitled

a guest Oct 3rd, 2017 54 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System;
  2. using NSubstitute;
  3. using Xunit;
  4.  
  5. namespace tdd_mvvm
  6. {
  7.   public class LoginPresenterTests
  8.   {
  9.     [Fact]
  10.     public void SubscribeToViewsLoginSubmit()
  11.     {
  12.       var view = Substitute.For<IPresenterView>();
  13.  
  14.       var subject = new LoginPresenter(view, null);
  15.  
  16.       view.Received().SubmitLogin += subject.OnLoginSubmitted;
  17.     }
  18.  
  19.     [Fact]
  20.     public void WhenLoginSubmitIsTriggeredGrabUsernameAndPassword()
  21.     {
  22.       var view = Substitute.For<IPresenterView>();
  23.       var subject = new LoginPresenter(view, null);
  24.  
  25.       view.SubmitLogin += Raise.EventWith(new object(), new EventArgs());
  26.  
  27.       var assertUsername = view.Received().Username;
  28.       var assertPassword = view.Received().Password;
  29.     }
  30.  
  31.     [Fact]
  32.     public void ForwardUsernameAndPasswordToTheAuthenticationServie()
  33.     {
  34.       var view = Substitute.For<IPresenterView>();
  35.       var authService = Substitute.For<IAuthenticationService>();
  36.       var subject = new LoginPresenter(view, authService);
  37.      
  38.       view.SubmitLogin += Raise.EventWith(new object(), new EventArgs());
  39.  
  40.       authService.Received().Authenticate(Arg.Any<string>, Arg.Any<string>);
  41.     }
  42.   }
  43.  
  44.   public interface IAuthenticationService
  45.   {
  46.     void Authenticate(string username, string password);
  47.   }
  48.  
  49.   public interface IPresenterView
  50.   {
  51.     event EventHandler SubmitLogin;
  52.     string Username { get; }
  53.     string Password { get; set; }
  54.   }
  55.  
  56.   public class LoginPresenter
  57.   {
  58.     private readonly IPresenterView _view;
  59.  
  60.     public LoginPresenter(IPresenterView view, IAuthenticationService authService)
  61.     {
  62.       _view = view;
  63.       _view.SubmitLogin += OnLoginSubmitted;
  64.     }
  65.  
  66.     public void OnLoginSubmitted(object source, EventArgs args)
  67.     {
  68.       var username = _view.Username;
  69.     }
  70.   }
  71. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top