According to StackOverflow, C# is one of the most-loved programming languages. And I completely understand that—it is powerful, easy to learn and consistently improving and developing. It is a living language. :)
The last couple of years, there were new features added to the languages, and the new versions keep coming up—C# 7, C# 8, C# 9.
As you know, we at Progress Telerik are proud that our products are always in sync with the latest things in the .NET world, and C# 9 and JustMock are no exception.
Most of the new features are easy to use in unit tests and in mocking, but there are some interesting things that I would like to show you so you can easily use the C# features in the unit testing:
To illustrate these, we will use a class Foo.
Note: To run the examples, you need to download and install JM from here.
Starting with C# 8.0, you can create and consume streams asynchronously. A method that returns an asynchronous stream has three specifics:
In the example below, you can see an example of such a method along with an example of how you could mock it:
Init only setters provide consistent syntax to initialize members of an object. Property initializers make it clear which value is setting which property. The downside is that those properties must be settable. Starting with C# 9.0, you can create
init accessors instead of
set accessors for properties and indexers.
How do you mock it when you write a test? Using Mock.NonPublic.ArrangeSet method of JustMock.
As the docs say, basically, you look at a given structure and, based on the way it looks, you identify it and you then can immediately use it. If you get a bag of fruit, you look down and immediately see the difference between the apples and the pears.
To show you how such functionality can be mocked with the InRange method, this is our sample of pattern matching and then the test below:
I know that reading code is not the same as running it, so to use it and play with the samples, follow the steps below:
For more tricks and trips on how to use the API of JustMock to write tests fast, read our Cheat Sheet.
If you are intrigued (hopefully you are 🤞), I’d be more than happy to hear your honest feedback in the comments.
Regardless of the above “cases,” don’t be shy to:
You won’t regret it.
Rossitza Fakalieva is a Technical Manager, Microsoft MVP in Developer Technologies and a Director of the Bulgarian chapter of the global Women Who Code organization. She previously worked on the Telerik engineering team and defines herself as .NET enthusiast. She loves to empower others to grow in their career and in the tech field—by teaching, by delivering courses and presentations, and as part of her daily job.
Subscribe to be the first to get our expert-written articles and tutorials for developers!
All fields are required