Unit Tests are a way to ensure the logic of your classes are producing the expected results. Those tests can be run from Visual Studio without having to load up the entire application manually then navigating to a specific section and trying to reproduce various scenarios.
There are some widely used naming conventions, for the assembly name it is often used as:
[AssemblyName].Tests
The test files are often named as:
[ClassName]Tests
And finally the methods are named as:
UnitOfWork_StateUnderTest_ExpectedBehavior
So if I have, for example:
- CodingWise.Core
- |- Roman.cs
- |- ConvertFrom // method
- |- ConvertTo // method
- CodingWise.Core.Tests
- |- RomanTests.cs
- |- ConvertFrom_Decimal1_ReturnsI // method
- |- ConvertFrom_Decimal9_ReturnsIX // method
- |- ConvertTo_RomanIV_Returns4 // method
- ...
- TestClass
- TestMethod
- [TestClass]
- public class RomanConverterTests
- {
- [TestMethod]
- public void ConvertFrom_Decimal1_ReturnsI()
- {
- Assert.AreEqual("I", Roman.ConvertFrom(1));
- }
- }
There are the following four other attributes for methods that are not required but they can be quite useful:
ClassInitialize runs once before all tests.
- ClassInitialize Runs once before all tests.
- ClassCleanUp Runs once after all tests.
- TestInitialize Runs before you run each test.
- TestCleanUp Runs after you run each test.
- [TestClass]
- public class UserServiceTests
- {
- private ApplicationContext db;
- [TestInitialize]
- public void TestInitialize()
- {
- db = new ApplicationContext();
- db.Database.BeginTransaction();
- }
- [TestCleanup]
- public void TestCleanUp()
- {
- db.Database.CurrentTransaction.Rollback();
- db.Dispose();
- }
- [TestMethod]
- public void Save_AllFields_Completes()
- {
- var userService = new UserService { Context = db };
- var user = new User();
- user.Username = "brunolm";
- user.Name = "BrunoLM";
- user.Email = "[email protected]";
- userService.Save(user);
- }
- }
- IItem
The following example uses Moq.
- public class CartService
- {
- public ApplicationContext Context { get; set; }
- public IList<IItem> Items { get; set; }
- public CartService()
- {
- Items = new List<IItem>();
- }
- public void Add(IItem item)
- {
- if (!item.Buyable)
- throw new ArgumentException("This item can't be bought.", "item");
- Items.Add(item);
- }
- }
- public interface IItem
- {
- bool Buyable { get; set; }
- }
- [TestClass]
- public class CartServiceTests
- {
- [TestMethod]
- [ExpectedException(typeof(ArgumentException))]
- public void Add_NonBuyable_Throws()
- {
- var cartService = new CartService();
- var mock = new Mock<IItem>();
- mock.SetupProperty<bool>(item => item.Buyable, false);
- cartService.Add(mock.Object);
- }
- [TestMethod]
- public void Add_Buyable_Includes()
- {
- var cartService = new CartService();
- var mock = new Mock<IItem>();
- mock.SetupProperty<bool>(item => item.Buyable, true);
- cartService.Add(mock.Object);
- Assert.AreEqual(1, cartService.Items.Count);
- }
- }
On MVC you will find examples of mocks for controller contexts, HTTP contexts and so on.
So always remember to choose good names, limit the scope of your test and use mocks with responsibility.

Santhakumar MunuswamyPosted Jun 22, 2015, 2:16 PM
Thanks for nice article
Debasis SahaPosted Jun 22, 2015, 5:41 AM
Good
Sibeesh VenuPosted Jun 22, 2015, 5:02 AM
Good One.
Gopi ChandPosted Jun 22, 2015, 1:32 AM
Great article
Jaipal ReddyPosted Jun 22, 2015, 1:31 AM
thank you.