Mocking
Mocking Properties
Mocking Public Properties
Example:
public virtual Owner User{get; set;}
...
var prodx = new Mock<Product>();
prodx.Setup(p=>p.Owner).Returns(fakeTestOwner);
Mocking Protected Properties
Example:
protected virtual List<User> UsersList{get; set;}
...
students.Protected().Setup<List<User>>("UsersList").Returns(fakeTestUsersList);
Mocking Complex Property Types
Mocking Collection Properties
You cannot mock a complex property type such as a List-Property like a simple property e. g. a property of DateTime type:
You cannot mock a list (collection) property like this!
var owner = new Mock<User>() { CallBase = true };
owner.Object.Id = Guid.NewGuid();
owner.Object.Login = "Satoshi";
owner.Object.Password = "123456";
owner.Object.Roles = assignedRoles;
Instead you have to mock such collection property in a similar fashion like a method:
You Can mock a list (collection) propertylike this!
var owner = new Mock<User>() { CallBase = true };
owner.Object.Id = Guid.NewGuid();
owner.Object.Login = "Satoshi";
owner.Object.Password = "123456";
owner.Setup(o => o.Roles).Returns(assignedRoles);
The property has to be virtual of course!
Mocking Methods
Mocking Public method
var owner = new Mock<User>() { CallBase = true };
owner.Setup(o => o.AddRole(newRole));
...
owner.Verify(o => o.AddRole(newRole),Times.Once());
Example with a return value:
owner.Setup(o => o.AddRole(It.IsAny<List<Role>>())).Returns<
List<Role>
>(rolesList);
Mocking passed parameters
Mocking Functions as Parameters
The following function call
owner.GetActivityReport(generateReport());
can be mocked as follows:
var owner = new Mock<User>();
ReportGenerator reportGenerator = null;
owner.Setup(o => o.GetActivityReport(It.IsAny<ReportGenerator>())).Callback<ReportGenerator>((r) =>
{
reportGenerator = r;
});
Protected method
Lets assume we have a class Order with a protected method confirm with parameters payment method and delivery address:
order.confirm(paymentMethod, deliveryAddress);
Such a protected method can be mocked as follows:
var newOrder = new Mock<Order>() { CallBase = true };
newOrder.Protected().Setup<bool>("confirm",ItExpr.IsAny<EPaymentMethod>(),ItExpr.IsAny<Address>()).Returns(true);
newOrder.Protected().Verify("confirm", Times.Exactly(1), ItExpr.IsAny<EPaymentMethod>(), ItExpr.IsAny<Address>());
Remark: The parameter placeholders are necessary otherwise would moq be unable to recognize the signature of the method we plan to mock.
Executing Inline Anonymus method
Example without passed parameter:
customer.Protected().Setup<List<Order>>(
"Orders").Callback(() =>
{
fakeOrders.DoSomething();
}).Returns(fakeOrders);
Example with passed Parameter:
orders.Setup(o => o.Delete(It.IsAny<Order>()))
.Callback<Order>((r) =>
{
deletedCount++;
deletedOrder = r.Id;
});
Example without returned object:
var order = new Mock<Order>() { CallBase = true };
order.Protected().Setup("Send").Verifiable();
Example with returned object:
currentUser.Protected().Setup<List<Role>>(
"Roles").Callback(() =>
{
fakeRoles.DoSomething();
}).Returns(fakeRoles);
Checking
Checking calls to a public method
var product = new Mock<Product>() { CallBase = true };
order.Verify(o => o.SetPrice(newPrice),Times.Once());
Check method call Regardless of parameter passed
product.Verify(p=> p.SoldTo(It.IsAny<Customer>()), Times.Never());
Checking calls to a protected method
var rule = new Mock<Rule>() { CallBase = true };
rule.Protected().Setup<bool>("IsActiveClient",ItExpr.IsAny<Client>(),ItExpr.IsAny<Site>()).Returns(true);
...
rule.Protected().Verify("IsActiveClient", Times.Exactly(3), ItExpr.IsAny<Client>(), ItExpr.IsAny<Site>());
Remark: The parameter placeholders are necessary otherwise moq won't recognize the signature of the method!
Checking passed paramters
Checking passed parameters with void return
Product p1 = null;
Vendor v1 = null;
Customer c1 = null;
Order o1 = null;
transaction1.Protected().Setup("ExecuteTransaction",
ItExpr.IsAny<Product>(),
ItExpr.IsAny<Vendor>(),
ItExpr.IsAny<Customer>(),
ItExpr.IsAny<Order>())
.Callback<Product, Vendor, Customer, Order>
((p, v, c, o) =>
{
p1 = p;
v1 = v;
c1 = c;
o1 = o;
});
. . .
Assert.AreEqual("Dell", v1.Title);
Checking passed parameters with return value
DateTime? deliveryDate = null;
order1.Protected().Setup<DateTime?>
("getNextDeliveryDate", ItExpr.IsAny<Guid>(), ItExpr.IsAny<DateTime?>())
.Callback<EDeliveryMethod, string>((deliveryMethod, customerAddress) =>
{
deliveryDate = DeliveryHelper.GetEstimatedDeliveryDuration(product1, deliveryMethod, customerAddress);
}
).Returns(deliveryDate);
to be completed