Real Properties With Moq
They're real and they're spectacular!
- Keith Ott
- Tutorials
- April 19, 2019
- 2 Minutes
I was recently using the C# Moq mocking library, and I needed to not only initialize a property to a value, but I needed the property to be updated by the code.
The basic setup of my interfaces was like this:
interface IAccount
{
IList<ITransaction> Transactions { get; set; }
}
interface ITransaction
{
DateTime Timestamp { get; set; }
}
And the ASP.NET MVC controller code under test was doing this:
IAccount account;
account.Transactions = account.Transactions.OrderBy(x => x.Timestamp).ToList();
var viewModel = new OurViewModel();
viewModel.Account = account;
My test class initialized an IAccount like this:
var transactions = new List<ITransaction>
{
new Transaction { Timestamp = new DateTime(2019, 03, 14) },
new Transaction { Timestamp = new DateTime(2019, 01, 01) }
}
var mockIAccount = new Mock<IAccount>();
mockIAccount.Setup(x => x.Transactions).Returns(transaction);
I wanted to verify that the controller code would sort the Transactions correctly. I could try abusing VerifySet
, but that would mean I’m testing the implementation rather than the outcome (not something I wanted to do). What I really needed was Moq to allow setting and reading this property. As it turns out, Moq can do this with SetupProperty
!
For Moq to make a working property, simply call SetupProperty
, optionally passing a second parameter that will set the initial property value.
mockIAccount.SetupProperty(x => x.Transactions, transactions);
Now, after we call out controller code, we can access the contents of this property in our test:
OurViewModel viewModel = ourController.CallUnderTest();
Assert.AreEqual(new DateTime(2019, 01, 01), viewModel.Account.Transactions[0].Timestamp);
Assert.AreEqual(new DateTime(2019, 03, 14), viewModel.Account.Transactions[1].Timestamp);
Pretty slick if you ask me!
P.S. Yes, I realize the real issue here is our controller shouldn’t be modifying the order of the Transactions in our Account class. No one ever said real software development was perfect.
Share this:
You may also like:
New Web App: Simple Stock Ticker
Check stock prices with this React web app Read more...
New Windows Game: Soda Chess
Command-line chess that supports all rules and any combination of human and AI players Read more...
New Web App: External Battery Backup
Store retro video game passwords and high scores in this web app Read more...