Can we write the mock test cases for .NET MVC application model classes?
-
Monday, November 14, 2011 3:57 PM
Hi,
We are writing the unit testcases(mock using Moq framework) for our .NET MVC application.
I have 2 questions :
1. Can we write Mock testcases for .NET MVC model classes?
2. I have done the mock test for Get_data method. But i could not do mock test the save method using Moq framework. Can you please help with any samples. And again i am mentioning here, i am trying to write a mock test for model classes. Is it possible?
Thanks in Advance.
Regards,
Palani.
- Edited by Palanikumar M Monday, November 14, 2011 3:58 PM
- Edited by Palanikumar M Monday, November 14, 2011 5:01 PM
- Edited by Palanikumar M Monday, November 14, 2011 6:23 PM
All Replies
-
Tuesday, November 15, 2011 9:04 AM
Yes sure,
But you have to remember that by using Mock you isolate the model from the underline layers so you just test the interaction with the model not the DB
[TestMethod] public void MyTestMethod() { // Arrange Product prod = new Product {Name = "prod1", Price = 275M}; Mock<IProductRepository> mock = new Mock<IProductRepository>(); mock.Setup(m => m.SaveProduct()).Returns(true); var target = mock.Object // Act Var result = target.Save(); // Assert Assert.AreEqual(true, result);
Actually this is a simple example but not a real one because in my real application I didn't make repository return true or false I just throw exception but it just to simplify the idea to you
My advice to you start reading more deeply about unit testing, you can try faking framework like DevMgicFake it will help you doing some kind of faking and unit testing approaches see this link : DevMagicFake on CodePlexThanks
We are volunteers, if the reply help you mark it as your answer. thanks!!
Blog: http://mohamedradwan.wordpress.com

