Skip to content

Instantly share code, notes, and snippets.

@Vannevelj
Created October 30, 2014 16:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Vannevelj/ad07bbd58946c4d909d9 to your computer and use it in GitHub Desktop.
Save Vannevelj/ad07bbd58946c4d909d9 to your computer and use it in GitHub Desktop.
Tests for Web Api 2 responses, Web Api 2 routes & Entity-Framework
namespace BackendTests
{
[TestClass]
public class UserControllerTests
{
private DatabaseContext _context;
[TestInitialize]
public void Initialize()
{
var connection = Effort.DbConnectionFactory.CreateTransient();
_context = new DatabaseContext(connection);
}
[TestMethod]
public void Register_WithNewUserInformation_InsertsUser()
{
var userController = new UserController(new UserRepository(_context));
const string email = "test@test.com";
var newUserInfo = new UserRegisterModel
{
Email = email,
Password = "test",
FirstName = "Abdul",
LastName = "Smith"
};
userController.Register(newUserInfo);
Assert.IsNotNull(_context.Users.FirstOrDefault(x => x.Information.Email == email));
}
[TestMethod]
public void Register_WithNewUserInformation_ReturnsHttpCode200()
{
var userController = new UserController(new UserRepository(_context));
const string email = "test@test.com";
var newUserInfo = new UserRegisterModel
{
Email = email,
Password = "test",
FirstName = "Abdul",
LastName = "Smith"
};
var response = userController.Register(newUserInfo) as OkNegotiatedContentResult<UserViewModel>;
Assert.IsNotNull(response);
Assert.AreEqual(email, response.Content.Information.Email);
}
[TestMethod]
public void Register_WithExistingUserInformation_DoesNotAlterExistingUser()
{
var userController = new UserController(new UserRepository(_context));
const string email = "test@test.com";
const string originalFirstname = "Abdul";
const string newFirstName = "John";
var existingUserInfo = new UserRegisterModel
{
Email = email,
Password = "test",
FirstName = originalFirstname,
LastName = "Smith"
};
userController.Register(existingUserInfo);
var newUserInfo = new UserRegisterModel
{
Email = email,
Password = "test",
FirstName = newFirstName,
LastName = "Smith"
};
userController.Register(newUserInfo);
Assert.AreEqual(originalFirstname, _context.Users.FirstOrDefault(x => x.Information.Email == email).Information.FirstName);
}
[TestMethod]
public void Register_WithExistingUserInformation_ReturnsHttpCode409()
{
var userController = new UserController(new UserRepository(_context));
const string email = "test@test.com";
var existingUserInfo = new UserRegisterModel
{
Email = email,
Password = "test",
FirstName = "Abdul",
LastName = "Smith"
};
userController.Register(existingUserInfo);
var newUserInfo = new UserRegisterModel
{
Email = email,
Password = "test",
FirstName = "Abdul",
LastName = "Smith"
};
var response = userController.Register(newUserInfo) as ConflictResult;
Assert.IsNotNull(response);
}
[TestMethod]
public void Register_WithCorrectRoute_CallsAppropriateMethod()
{
var config = new HttpConfiguration();
WebApiConfig.Register(config);
config.EnsureInitialized();
const string route = "/api/users/register";
RouteAssert.HasApiRoute(config, route, HttpMethod.Post);
config.ShouldMap(route).To<UserController>(HttpMethod.Post, x => x.Register(null));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment