Group testing of CRUD operations in visual studio 2012

I am testing the create class in Visual Studio 2012

My controller class:

public ActionResult Create() { return View(); } // // POST: /Member/Create [HttpPost] public ActionResult Create(Member member) { if (ModelState.IsValid) { db.Members.Add(member); db.SaveChanges(); return RedirectToAction("Index"); } return View(member); } 

And the test class:

 [TestClass] public class MemberTest { [TestMethod] public void Create(Member mem) { mem.MemID = 123; mem.MemName = "sruthy"; /// dont know what is writing. } } 

SampleDataContext.cs

 public class SampleDataContext:DbContext { public DbSet<Member> Members { get; set; } protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Conventions.Remove<PluralizingTableNameConvention>(); } } 

I am stuck in a test case, please help me.

+6
source share
2 answers

First, create an abstraction for your data access code (mocking DbContext is not a very convenient thing):

 public interface IMemberRepository { void Add(Member member); } 

and depend on your controller.

 public MemberController(IMemberRepository repository) { this.repository = repository; } 

This will make it easy to get a data access code. Next, write down the tests that verify the behavior of the controller (here I use NUnit and Moq):

 private MemberController controller; private Mock<IMemberRepository> repositoryMock; private Member member; [SetUp] public void Setup() { repositoryMock = new Mock<IMemberRepository>(); controller = new MemberController(repositoryMock.Object); member = new Member { MemID = 123, MemName = "sruthy" }; } [Test] public void ShouldCreateMemberWhenItIsValid() { var result = (RedirectToRouteResult)controller.Create(member); Assert.That(result.RouteValues["action"], Is.EqualTo("Index")); repositoryMock.Verify(r => r.Add(member)); } [Test] public void ShouldNotCreateMemberWhenItIsNotValid() { controller.ModelState.AddModelError("MemName", "Something wrong"); var result = (ViewResult)controller.Create(member); Assert.That(result.ViewName, Is.Empty); } 

And write the implementation:

 [HttpPost] public ActionResult Create(Member member) { if (ModelState.IsValid) { repository.Add(member); return RedirectToAction("Index"); } return View(member); } 
+5
source

In unit testing, I realized: "test only what your method does." Therefore, I think you need to check that your method succeeds:

  • ModelState.IsValid

  • db.Members.Add (member)

  • db.SaveChanges ()

But not good ModelState or DbContext behavior. They are tested in their unit tests. You must state that only the call is completed.

To perform such a test, you must use the Dependency injection pattern and replace the real DbContext with mocks. These bullying simply claims that the call is well-executed without involving a real dbContext.

I am not a unit testing specialist, but I think you need to think about your whole architecture to separate your objects. This allows you to replace real objects with mocks.

+3
source

Source: https://habr.com/ru/post/954928/


All Articles