Monthly Archives

2 Articles
Mocking objects with Moq and XUnit in .Net Core

Mocking objects with Moq and XUnit in .Net Core

Moq library 4.5 and above supports mocking objects in net Core. This article explains the step by step process that needed to Moq an object in net core. Mocking objects comes in handy when unit testing data store, where a Database can be mocked so that no data is added or modified in Database while unit testing source code.

Creating a test project

For the simplicity of explaining, I’m started with creating a XUnit test project in Visual Studio 2017 15.3

Add Moq package into the project

Read More

How to check if an object is null in C#?

How to check if an object is null in C#?

Usual methods to check a type is null will result in Null reference exception if an object is checked for null. This post compiles all good and bad methods.

Following methods of checking if an object is null will result in a NullreferenceException.

Wrong method #1

object simpleObject = null;
If(string.IsNullOrEmpty(simpleObject.toString())
{
 // do something if object is null
}
else
{
 //do somethingif object is not null
}

Wrong method #2

Read More