Skip to content

Instantly share code, notes, and snippets.

@praveensewak
Last active September 12, 2016 02:45
Show Gist options
  • Save praveensewak/c3e1eee04a088a7644fb5f65eba9ec76 to your computer and use it in GitHub Desktop.
Save praveensewak/c3e1eee04a088a7644fb5f65eba9ec76 to your computer and use it in GitHub Desktop.
Unit Testing Helpers
namespace MvcApplication.Tests.Controllers
{
using System.Web.Mvc;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using MvcApplication.Controllers;
[TestClass]
public class AdminControllerTest
{
IUserService _userService;
[TestInitialize]
public void Init()
{
var mockUsers = (new List<User>
{
new User{ Id = 1, Username = "admin", Name = "Website Admin" },
new User{ Id = 2, Username = "praveensewak", Name = "Praveen Sewak" },
}).Select(x => x).AsQuerable();
var mockDataRepo = new Mock<IUserRepository>();
mockDataRepo.Setup(d => d.GetAll()).Returns(mockUsers);
mockDataRepo.Setup(d => d.Get(It.IsAny<int>())).Returns(mockUsers.Single(u => u.Id == 2));
_userService = new UserService(mockDataRepo.Object);
}
[TestMethod]
public void TestIndexView()
{
// Arrange
var controller = new AdminController(_userService);
// Act
var result = controller.Details(2) as ViewResult;
// Assert
Assert.AreEqual("Details", result.ViewName);
}
[TestMethod]
public void TestDetailView()
{
// Arrange
var controller = new AdminController(_userService);
// Act
var result = controller.Details(2) as ViewResult;
// Assert
Assert.IsNotNull(result);
var model = (ViewModel)result.Model;
Assert.AreEqual(2, model.Id);
}
}
}
// TestExtensions.cs
namespace MvcApplication.Tests
{
using System;
using NUnit.Framework;
public static class TestExtensions
{
public static T ShouldNotNull<T>(this T obj)
{
Assert.IsNull(obj);
return obj;
}
public static T ShouldNotNull<T>(this T obj, string message)
{
Assert.IsNull(obj, message);
return obj;
}
public static T ShouldNotBeNull<T>(this T obj)
{
Assert.IsNotNull(obj);
return obj;
}
public static T ShouldNotBeNull<T>(this T obj, string message)
{
Assert.IsNotNull(obj, message);
return obj;
}
public static T ShouldEqual<T>(this T actual, object expected)
{
Assert.AreEqual(expected, actual);
return actual;
}
public static void ShouldEqual(this object actual, object expected, string message)
{
Assert.AreEqual(expected, actual);
}
public static Exception ShouldBeThrownBy(this Type exceptionType, TestDelegate testDelegate)
{
return Assert.Throws(exceptionType, testDelegate);
}
public static void ShouldBe<T>(this object actual)
{
Assert.IsInstanceOf<T>(actual);
}
public static void ShouldBeNull(this object actual)
{
Assert.IsNull(actual);
}
public static void ShouldBeTheSameAs(this object actual, object expected)
{
Assert.AreSame(expected, actual);
}
public static void ShouldBeNotBeTheSameAs(this object actual, object expected)
{
Assert.AreNotSame(expected, actual);
}
public static T CastTo<T>(this object source)
{
return (T)source;
}
public static void ShouldBeTrue(this bool source)
{
Assert.IsTrue(source);
}
public static void ShouldBeFalse(this bool source)
{
Assert.IsFalse(source);
}
public static void AssertSameStringAs(this string actual, string expected)
{
if (!string.Equals(actual, expected, StringComparison.InvariantCultureIgnoreCase))
{
var message = string.Format("Expected {0} but was {1}", expected, actual);
throw new AssertionException(message);
}
}
}
}
namespace MvcApplication.Tests
{
using MvcApplication.Domain;
using NUnit.Framework;
[TestFixture]
public class ProductTests
{
[Test]
public void Can_parse_tags()
{
var product = new Product
{
Tags = "tag1, tag2, tag 4 5, "
};
var tags = product.ParseTags();
tags.Length.ShouldEqual(3);
tags[0].ShouldEqual("tag1");
}
}
}
// TestsBase.cs
namespace MvcApplication.Tests
{
using System.Security.Principal;
using NUnit.Framework;
using Rhino.Mocks;
public abstract class TestsBase
{
protected MockRepository mocks;
[SetUp]
public virtual void SetUp()
{
mocks = new MockRepository();
}
[TearDown]
public virtual void TearDown()
{
if (mocks != null)
{
mocks.ReplayAll();
mocks.VerifyAll();
}
}
protected static IPrincipal CreatePrincipal(string name, params string[] roles)
{
return new GenericPrincipal(new GenericIdentity(name, "TestIdentity"), roles);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment