Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you use Moq to mock a simple interface?

Okay, I have a business logic class like this:

Note: For context, Vendor Briefs are simple entities that describe a "download" for a PDF document.

/// <summary> /// Houses business level functions for dealing with vendor briefs. /// </summary> public class VendorBriefController : IVendorBriefController {     /// <summary>     /// Vendor brief controller requires an instance of IVendorBriefRepository.     /// </summary>     IVendorBriefRepository _vendorBriefRepository;      /// <summary>     /// Initializes an instance of VendorBriefController.     /// </summary>     public VendorBriefController(IVendorBriefRepository vendorBriefRepository)     {         _vendorBriefRepository = vendorBriefRepository;     }      /// <summary>     /// Get a list of string filters for vendor briefs.     /// </summary>     /// <returns>A list of string filters.</returns>     public dynamic GetFilters()     {         List<string> filters = new List<string>         {             "All",             "Active",             "Inactive"         };         return filters;     }      /// <summary>     /// Retrieve vendor brief entity from the repository by its unique ID.     /// </summary>     /// <param name="Id">The unique ID of the vendor brief.</param>     /// <returns>A vendor brief entity.</returns>     public VendorBrief GetVendorBriefForEditing(int Id)     {         var vendorBrief = _vendorBriefRepository.GetVendorBrief(Id);         return vendorBrief;     }      /// <summary>     /// Get a dynamic list of vendor briefs from the repository based on the supplied filter.     /// </summary>     /// <param name="filter">The filter to be used when retrieving vendor briefs.</param>     /// <returns>A dynamic sorted & filtered list of vendor briefs to be displayed in a grid view.</returns>     public dynamic GetVendorBriefList(string filter)     {         IEnumerable<VendorBrief> results = _vendorBriefRepository.GetVendorBriefs();         switch (filter)         {             default:                 results = _vendorBriefRepository.GetVendorBriefs();                 break;             case "Active":                 results = _vendorBriefRepository.GetVendorBriefs(true);                 break;             case "Inactive":                 results = _vendorBriefRepository.GetVendorBriefs(false);                 break;         }         return from x in results                orderby x.DisplayOrder                select new                {                    ID = x.VendorBriefID,                    Title = x.Title,                    Active = x.IsActive,                    DisplayOrder = x.DisplayOrder                };     }      /// <summary>     /// Save changes to the underlying repository in order to persist changes made to self-tracking vendor brief entities.     /// </summary>     /// <param name="vendorBrief"></param>     public void EditVendorBrief(VendorBrief vendorBrief)     {         _vendorBriefRepository.SaveChanges();     }      /// <summary>     /// Remove a vendor brief from the underlying repository.     /// </summary>     /// <param name="vendorBrief">The vendor brief to be removed.</param>     public void DeleteVendorBrief(VendorBrief vendorBrief)     {         _vendorBriefRepository.DeleteVendorBrief(vendorBrief);         _vendorBriefRepository.SaveChanges();     }      /// <summary>     /// Add a vendor brief to the underlying repository.     /// </summary>     /// <param name="vendorBrief">The vendor brief to be added.</param>     public void AddVendorBrief(VendorBrief vendorBrief)     {         _vendorBriefRepository.AddVendorBrief(vendorBrief);         _vendorBriefRepository.SaveChanges();     } } 

I am taking my first steps into unit testing and I'm learning about Moq. I don't want a whole unit test class written for this (unless you feel like it of course :P) but a simple sample will do. I'm assuming I need to "mock" IVendorBriefRepository so that I can pass it into the constructor when building my controller (not to be confused with mvc controllers), but I'm not sure how to do it. A sample using some of my own code will really help me get started.

Thanks in advance!

like image 407
Chev Avatar asked Apr 11 '11 20:04

Chev


People also ask

Can you mock an interface?

The Mockito. mock() method allows us to create a mock object of a class or an interface. We can then use the mock to stub return values for its methods and verify if they were called.

Can you mock a class with Moq?

You can use Moq to create mock objects that simulate or mimic a real object. Moq can be used to mock both classes and interfaces. However, there are a few limitations you should be aware of. The classes to be mocked can't be static or sealed, and the method being mocked should be marked as virtual.

What can be mocked with Moq?

Unit testing is a powerful way to ensure that your code works as intended. It's a great way to combat the common “works on my machine” problem. Using Moq, you can mock out dependencies and make sure that you are testing the code in isolation.

How does Moq mock work?

Mock objects allow you to mimic the behavior of classes and interfaces, letting the code in the test interact with them as if they were real. This isolates the code you're testing, ensuring that it works on its own and that no other code will make the tests fail.


Video Answer


1 Answers

Something like this would test DeleteVendorBrief, for example.

Mock<IVendorBriefRepository> mock = new Mock<IVendorBriefRepository>();  VendorBriefController controller = new VendorBriefController(mock.Object);  VendorBrief brief = new VendorBrief();  controller.DeleteVendorBrief(brief);  mock.Verify(f=>f.DeleteVendorBrief(brief)); mock.Verify(f=>f.SaveChanges()); 
like image 117
Brook Avatar answered Sep 21 '22 15:09

Brook