jeudi 29 octobre 2015

Nunit Testing with Moq

I recently started learning Unit Test in MVC and used NUnit Framework for Test Cases. My problem is, i cannot understand for what should i write Test case. Imagine i have CRUD operation and i want to Test them, so what should be my Test case condition.

Here is my Interface class:

public interface IUserRepository 
{ 
    //Creating Single User Records into database using EF.
    bool CreateUser(tbl_Users objUser);

    //Updating Single User Records into database using EF.
    void UpdateUser(tbl_Users objUser);

    //Deleting Single User Records from database using EF.
    bool DeleteUser(long IdUser);
}

Here is my Repository Class:

public class UserRepository : IUserRepository
{
    DBContext objDBContext = new DBContext();      

    /// <summary>
    /// Creating new User Record into Database
    /// </summary> 
    /// <param name="objUser"></param>
    public bool CreateUser(tbl_Users objUser)
    {
        bool blnResult = false;
        objUser.MiddleName = string.IsNullOrEmpty(objUser.MiddleName) ? string.Empty : objUser.MiddleName.Trim();
        objUser.Photo = string.Empty;
        objUser.Approved = false;
        objUser.CreatedDate = DateTime.Now;
        objUser.DeleteFlag = false;
        objUser.UpdBy = 0;
        objUser.UpdDate = DateTime.Now;
        objDBContext.tbl_Users.Add(objUser);
        blnResult = Convert.ToBoolean(objDBContext.SaveChanges());
        return blnResult;
    }

    /// <summary>
    /// Updating existing User Record into Database
    /// </summary> 
    /// <param name="objUser"></param>
    public void UpdateUser(tbl_Users objUser)
    {
        objUser.MiddleName = string.IsNullOrEmpty(objUser.MiddleName) ? string.Empty : objUser.MiddleName.Trim();
        objUser.Approved = true;
        objUser.UpdBy = objUser.IdUser;
        objUser.UpdDate = DateTime.Now;
        objDBContext.Entry(objUser).State = EntityState.Modified;
        objDBContext.SaveChanges();
    }

    /// <summary>
    /// Deleting existing User Record from Database
    /// </summary> 
    /// <param name="IdUser"></param>
    public bool DeleteUser(long IdUser)
    {
        bool blnResult = false;
        tbl_Users objUser = objDBContext.tbl_Users.Where(x => x.IdUser == IdUser).Single();
        objUser.ConfirmPassword = objUser.Password;
        objUser.UpdDate = DateTime.Now;
        objUser.DeleteFlag = true;
        blnResult = Convert.ToBoolean(objDBContext.SaveChanges());
        return blnResult;
    }        
}

And Here is My Controller class

public class UserController : Controller
{
    tbl_Users objUser = new tbl_Users();
    UserRepository Repository = new UserRepository();        

    [ValidateAntiForgeryToken]
    [HttpPost]
    public ActionResult Create(tbl_Users objUser)
    {
        if (ModelState.IsValid)
        {
            try
            {
                Repository.CreateUser(objUser);
                return RedirectToAction("Update", "User");
            }
            catch
            {
                return View();
            }
        }
        return View();
    }       

    [ValidateAntiForgeryToken]
    [HttpPost]
    public ActionResult Update(tbl_Users objUser)
    {
        Repository.UpdateUser(objUser);
        return View();
    }

    public ActionResult Delete(long IdUser = 0)
    {
        bool blnResult = Repository.DeleteUser(IdUser);
        if (blnResult)
        {
            return View("Delete");
        }
        else
        {
            return View();
        }
    }
}

Here are Test cases which i tried to Execute using Moq

[TestFixture]
public class UserControllerTest
{
    UserController Controller;

    [SetUp]
    public void Initialise()
    {
        Controller = new UserController();
    }       

    [Test]
    public void DeleteTest()
    {
        var ObjUser = new Mock<IUserRepository>();

        ObjUser.Setup(X => X.DeleteUser(It.IsAny<long>())).Returns(true);

        var Result = ObjUser.Object.DeleteUser(1);
        Assert.That(Result, Is.True);
    }        

    [Test]
    public void CreateTest()
    {
        tbl_Users User = new tbl_Users();
        Mock<IUserRepository> MockIUserRepository = new Mock<IUserRepository>();
        MockIUserRepository.Setup(X => X.CreateUser(It.IsAny<tbl_Users>())).Returns(true);

        var Result = MockIUserRepository.Object.CreateUser(User);

        Assert.That(Result, Is.True);
    }         

    [TearDown]
    public void DeInitialise()
    {
        Controller = null;
    }
}

Can anyone tell me, how to Write test cases for above Controller Action Method with brief description about test cases using Moq.

Aucun commentaire:

Enregistrer un commentaire