Skip to content

Instantly share code, notes, and snippets.

@scionwest
Last active August 29, 2015 14:12
Show Gist options
  • Save scionwest/752ab399db55edb40de8 to your computer and use it in GitHub Desktop.
Save scionwest/752ab399db55edb40de8 to your computer and use it in GitHub Desktop.
Unit Test w/ Mocks
[TestClass]
public class AccountServiceTests
{
private IAccountService accountService;
[TestInitialize]
public void Setup()
{
// Build our User Repository mock for the account service to use.
var userRepositoryMock = new Mock<IUserRepository>();
userRepositoryMock
.Setup(repository => repository.CreateUser(
It.IsAny<string>(),
It.IsAny<string>(),
It.IsAny<string>()))
.ReturnsAsync(new OperationResult(true));
userRepositoryMock
.Setup(repository => repository.RetrieveUser(
It.IsAny<string>(),
It.IsAny<string>()))
.Returns((string username, string password) =>
{
var user = new User { Username = username, CreatedDate = DateTime.Now, };
var taskCompletion = new TaskCompletionSource<OperationResult<User>>();
taskCompletion.SetResult(new OperationResult<User>(true, user));
return taskCompletion.Task;
});
var accountServiceMock = new Mock<IAccountService>();
accountServiceMock
.Setup(service => service.CreateAccount(
It.IsAny<User>(),
It.IsAny<string>(),
It.IsAny<string>()))
.Returns((User user, string password, string passwordConfirmation) =>
{
var taskCompletion = new TaskCompletionSource<OperationResult>();
taskCompletion.SetResult(new OperationResult(true));
return taskCompletion.Task;
});
var builder = new ContainerBuilder();
builder.RegisterInstance(accountServiceMock.Object).As<IAccountService>();
this.accountService = new AccountService(userRepositoryMock.Object);
}
[TestMethod]
[TestCategory("App Services")]
[ExpectedException(typeof(ArgumentNullException))]
public async Task Create_account_with_null_user_fails()
{
// Act
OperationResult result = await this.accountService.CreateAccount(null, "Password", "Password");
}
[TestMethod]
[TestCategory("App Services")]
public async Task Create_account_with_valid_credentials()
{
// Arrange
var user = new User { Username = "Unit Test", Email = "Something" };
// Act
OperationResult result = await this.accountService.CreateAccount(user, "Password", "Password");
// Assert
Assert.IsTrue(result.IsSuccessful);
}
[TestMethod]
[TestCategory("App Services")]
public async Task Create_account_with_empty_username_fails()
{
// Arrange
var user = new User { Username = string.Empty, Email = "Something" };
// Act
OperationResult result = await this.accountService.CreateAccount(user, "Password", "Password");
// Assert
Assert.IsFalse(result.IsSuccessful);
Assert.AreEqual("Username must not be empty.", result.Result.Trim());
}
[TestMethod]
[TestCategory("App Services")]
public async Task Create_account_with_empty_email_fails()
{
// Arrange
var user = new User { Username = "Unit Test", Email = string.Empty };
// Act
OperationResult result = await this.accountService.CreateAccount(user, "Password", "Password");
// Assert
Assert.IsFalse(result.IsSuccessful);
Assert.AreEqual("Email Address must not be empty.", result.Result.Trim());
}
[TestMethod]
[TestCategory("App Services")]
public async Task Create_account_with_empty_password_fails()
{
// Arrange
var user = new User { Username = "Unit Test", Email = "Something" };
// Act
OperationResult result = await this.accountService.CreateAccount(user, string.Empty, "Password");
// Assert
Assert.IsFalse(result.IsSuccessful);
Assert.AreEqual("Password must not be empty.", result.Result.Trim());
}
[TestMethod]
[TestCategory("App Services")]
public async Task Create_account_with_empty_password_confirmation_fails()
{
// Arrange
var user = new User { Username = "Unit Test", Email = "Something" };
// Act
OperationResult result = await this.accountService.CreateAccount(user, "Password", string.Empty);
// Assert
Assert.IsFalse(result.IsSuccessful);
Assert.AreEqual("You must confirm your password.", result.Result.Trim());
}
[TestMethod]
[TestCategory("App Services")]
public async Task Create_account_with_mismatched_passwords_fails()
{
// Arrange
var user = new User { Username = "Unit Test", Email = "Something" };
// Act
OperationResult result = await this.accountService.CreateAccount(user, "Password", "bad password");
// Assert
Assert.IsFalse(result.IsSuccessful);
Assert.AreEqual("Your passwords do not match.", result.Result.Trim());
}
[TestMethod]
[TestCategory("App Services")]
public async Task Signin_to_account_with_empty_username_fails()
{
// Act
OperationResult result = await this.accountService.Signin(string.Empty, "Password");
// Assert
Assert.IsFalse(result.IsSuccessful);
Assert.AreEqual("Username must not be empty.", result.Result.Trim());
}
[TestMethod]
[TestCategory("App Services")]
public async Task Signin_to_account_with_empty_password_fails()
{
// Act
OperationResult result = await this.accountService.Signin("Unit Test", string.Empty);
// Assert
Assert.IsFalse(result.IsSuccessful);
Assert.AreEqual("Password must not be empty.", result.Result.Trim());
}
[TestMethod]
[TestCategory("App Services")]
public async Task Signin_to_account_with_valid_credentials()
{
// Act
OperationResult<User> result = await this.accountService.Signin("Unit Test", "Password");
// Assert
Assert.IsTrue(result.IsSuccessful);
Assert.IsNotNull(result.Item);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment