using System; using System.Threading.Tasks; using Microsoft.VisualStudio.TestTools.UnitTesting; // Import XamChat Libraries using XamChat3.Mocks; using XamChat3.Interfaces; using XamChat3.ViewModels; namespace XamChat3.Tests { [TestClass] public class LoginViewModelTest { // Add fields for dependencies LoginViewModel viewModel; ISettings settings; // A method for performing test setup - initialisation steps [TestInitialize] public void Setup() { // Register and initialise the Service Container IoC dependencies. ServiceContainer.Register(() => new MockWebService { SleepDuration = 0 }); ServiceContainer.Register(() => new MockSettings()); // Retrieve injected dependencies from the Service Container IoC and store references to them. settings = ServiceContainer.Resolve(); viewModel = new LoginViewModel(); } [TestMethod] public async Task LoginSuccessful_WhenValidLoginDetails() { // Arrange viewModel.Username = "testuser"; viewModel.Password = "password"; // Act await viewModel.LoginAsync(); // Assert Assert.IsNotNull(settings.User); } // This test expects an exception to be returned, but it does not check the exception method. // To check agains the exception message you will need to write an extension method or SEE the next example // See more: https://stackoverflow.com/questions/20494105/expectedexception-assert // https://github.com/bbraithwaite/MSTestExtensions [TestMethod, ExpectedException(typeof(Exception))] public async Task LoginThrowsException_WhenNoLoginDetails() { // Arrange - nothing to arrange // Act - should throw an exception await viewModel.LoginAsync(); // Assert - nothing to assert as exception is expected in previous step } // Alternative test which checks the specific exception message returned. [TestMethod] public async Task LoginThrowsException_WhenUsernameIsBlank() { // Arrange Exception resultException = null; viewModel.Username = null; viewModel.Password = "password"; // Act - use try { } catch { } block to catch the expected exception try { await viewModel.LoginAsync(); } catch (Exception ex) { resultException = ex; } // Assert - make sure the message you are comparing with is what you have written in your LoginViewModel Assert.AreEqual("Username is blank.", resultException.Message); } [TestMethod] public async Task LoginThrowsException_WhenPasswordIsBlank() { // Arrange Exception resultException = null; viewModel.Username = "testuser"; viewModel.Password = null; // Act - use try { } catch { } block to catch the expected exception try { await viewModel.LoginAsync(); } catch (Exception ex) { resultException = ex; } // Assert - make sure the message you are comparing with is what you have written in your LoginViewModel Assert.AreEqual("Password is blank.", resultException.Message); } // This test will fail as the MockWebService does not currently have this functionality [TestMethod] public async Task LoginThrowsException_WhenUsernameIsInvalid() { // Arrange Exception resultException = null; viewModel.Username = "someuser"; viewModel.Password = "password"; // Act - use try { } catch { } block to catch the expected exception try { await viewModel.LoginAsync(); } catch (Exception ex) { resultException = ex; } // Assert - make sure the message you are comparing with is what you have written in your LoginViewModel Assert.AreEqual("Invalid login details.", resultException.Message); } // This test will fail as the MockWebService does not currently have this functionality [TestMethod] public async Task LoginThrowsException_WhenPasswordIsInvalid() { // Arrange Exception resultException = null; viewModel.Username = "testuser"; viewModel.Password = "nopassword"; // Act - use try { } catch { } block to catch the expected exception try { await viewModel.LoginAsync(); } catch (Exception ex) { resultException = ex; } // Assert - make sure the message you are comparing with is what you have written in your LoginViewModel Assert.AreEqual("Invalid login details.", resultException.Message); } } }