morales-franco / xUnit-Moq-Demo-2

Implementing unit testing using Moq

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

xUnit-Moq-Demo-2

Implementing unit testing using Moq

Inject mock dependencies by constructor

  1. Test classes that use injecting services by constructor using Moq - Mock Framework - to mock these services.

Mocking Methods

  1. Configure mock object method return values.
mockValidator.Setup(..).Returns(true);
  1. specific value (hardcoding value way)
x => x.IsValid("x")
  1. Argument Matching in mocked methods (avoid harcoding values in test methods)
It.IsAny
It.IsInRange
...
  1. Mock behaviours MockBehavior.Strict | MockBehavior.Loose (default)

  2. Mocking methods wit out parameters

Mocking Properties

1.Mocking properties to return a specific values

mockValidator.Setup(x => x.LicenseKey)
             .Returns("EXPIRED");
  1. Mocking properties to return a function value
  2. Property Hierarchies - Auto-mocking property hierarchies
mockValidator.Setup(x => x.Level1.Level2.Level3.Property)
             .Returns(GetPropertyValue);
  1. Default value strategies for Properties.
mockValidator.DefaultValue = DefaultValue.Mock | DefaultValue.Empty (default)| DefaultValue.Custom 

5.Tracking changes to mock properties values - by default the mock fw doesn't keep states of mock properties. For example, we mock a object with a enum property, if this property is changed in the test process when the object returns to the test method, the new state of this property is lost. We can tell to the FW that keeps the state of specified properties or all properties to TRACKING CHANGES to mock properties values

mockValidator.SetupAllProperties();

or

mockValidator.SetupProperty(x => x.PropertyToTrack);

Behaviour verification tests

  1. Behaviour Testing & State Based Testing

  2. Behaviour Testing - Checking if a method was called with a specified or any parameter

mockValidator.Verify(x => x.MethodA(It.IsNotNull<string>()));
//Checking the method A was called with a string value different to null
  1. Behaviour Testing - Checking if a method was never called with
mockValidator.Verify(x => x.MethodA(It.IsAny<string>()), Times.Never);
//Checking the method A was not called
  1. Behaviour Testing - Checking how many times the method A it was called
mockValidator.Verify(x => x.MethodA(It.IsNotNull<string>()), Times.Exactly(2));
  1. Behaviour Testing - Checking if a property was called
mockValidator.VerifyGet(x => x.ServiceInformation.License.LicenseKey, Times.Once);
  1. Behaviour Testing - Checking if a property was set once (in this case with any value)
mockValidator.VerifySet(x => x.PropertyA = It.IsAny<ValidationMode>(), Times.Once);

Other techniques to test mock objects

  1. Throwing exception from Mock objects
mockValidator.Setup(x => x.MethodA(It.IsAny<string>()))
             .Throws(new Exception("Custom message"));
//if the method MethodA() is called with any string, then the mock object will throw an exception
  1. Raising events from Mock objects
mockValidator.Raise(x => x.ValidatorLookupPerformed += null, EventArgs.Empty);
  1. Returning different results for sequence calls
mockValidator.SetupSequence(x => x.MethodA(It.IsAny<string>()))
            .Returns(false)
            .Returns(true);

4.Mocking members of a concrete class - Protected & virtual methods

About

Implementing unit testing using Moq


Languages

Language:C# 100.0%