Last active
August 29, 2015 14:22
-
-
Save kiichi/406746c2efd0d9e89d67 to your computer and use it in GitHub Desktop.
Unit Test with .NET MVC and Moq for JsonResult object
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.ComponentModel.DataAnnotations; | |
| using System.Collections.Generic; | |
| using System.Web.Mvc; | |
| namespace MoqTest { | |
| public class Patient { | |
| public string FirstName; | |
| public string LastName; | |
| [Required] | |
| public string MRN; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.Collections.Generic; | |
| using System.Linq; | |
| using System.Web; | |
| using System.Web.Mvc; | |
| namespace MoqTest.Controllers{ | |
| // Interface for dependency injection | |
| public interface IPatientRepository{ | |
| JsonResult Index (); | |
| } | |
| public class PatientController : Controller{ | |
| private readonly IPatientRepository mPatientRepository; | |
| public PatientController(IPatientRepository patientRepository) { | |
| mPatientRepository = patientRepository; | |
| } | |
| public JsonResult Index(){ | |
| // This is demo for Moq. | |
| // Test say, this data is from database | |
| var dbData = new Patient[]{ | |
| new Patient{ | |
| FirstName = "Kiichi", | |
| LastName = "Takeuchi", | |
| MRN = "1234" | |
| }, | |
| new Patient{ | |
| FirstName = "John", | |
| LastName = "Smith", | |
| MRN = "4254" | |
| } | |
| }; | |
| return Json(new { | |
| Status = "ok", | |
| Patients = dbData | |
| },JsonRequestBehavior.AllowGet); | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using NUnit.Framework; | |
| using System.Collections.Generic; | |
| using System.Web; | |
| using System.Web.Mvc; | |
| using System.Linq; | |
| using System.Text; | |
| using MoqTest; | |
| using MoqTest.Controllers; | |
| using Moq; | |
| namespace MoqTest.Tests { | |
| [TestFixture] | |
| public class PatientControllerTest { | |
| private Mock<IPatientRepository> patientRepoMock; | |
| private PatientController target; | |
| [TestFixtureSetUp] | |
| public void Init(){ | |
| patientRepoMock = new Mock<IPatientRepository>(); | |
| target = new PatientController(patientRepoMock.Object); | |
| } | |
| [Test] | |
| public void IndexTest () { | |
| var data =new JsonResult{Data = | |
| new {Status="ok", | |
| Patients= new Patient[] { | |
| new Patient { | |
| FirstName = "Kiichi", | |
| LastName = "Takeuchi", | |
| MRN = "1234" | |
| }, | |
| new Patient { | |
| FirstName = "John", | |
| LastName = "Smith", | |
| MRN = "4254" | |
| } | |
| } | |
| } | |
| }; | |
| patientRepoMock.Setup(r => r.Index()).Returns(data); | |
| var result = target.Index(); | |
| //-------------------------------------------------------------- | |
| // Asserting Method 1 - use support function to evaluate the dynamic type | |
| Assert.AreSame ("ok", GetVal<string> (result, "Status")); | |
| Assert.AreEqual(2, GetVal<Patient[]>(result, "Patients").Length); | |
| //-------------------------------------------------------------- | |
| // Asserting Method 2 - using dymanic but you have to remove the security | |
| // Add | |
| // [assembly: InternalsVisibleTo("MyProject.Tests")] | |
| // in AssemblyInfo.cs | |
| //dynamic jsonObject = result.Data; | |
| //Assert.AreSame("ok",(string)jsonObject.Status); | |
| //Assert.AreEqual(2, jsonObject.Patients.Length); | |
| } | |
| // Support Function to evaluate JsonResult | |
| private T GetVal<T>(JsonResult jsonResult, string propertyName){ | |
| var property = jsonResult.Data.GetType().GetProperties() | |
| .Where(p => string.Compare(p.Name, propertyName) == 0) | |
| .FirstOrDefault(); | |
| if (null == property) | |
| throw new ArgumentException("propertyName not found", "propertyName"); | |
| return (T)property.GetValue(jsonResult.Data, null); | |
| } | |
| } | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment