Category Archives

3 Articles
Most useful mocking tricks for MOQ and C#

Most useful mocking tricks for MOQ and C#

The following list is my most used mocking techniques to create, setup and verifies injected dependencies.

Create

This section lists all the most used ways of creating a mocked object of a dependancy

Create a mocked object of a dependency

var mockedSystemUnderTest = new Mock();

Setup

This section lists all the most used ways of setting up behaviors of a dependancy

Setup all properties in a mocked dependency

mockedSystemUnderTest.SetAllProperties();

Setup a specific property in a dependency

mockedSystemUnderTest.SetGet(prop=>prop.SomeProperty).Returns("SomeValue");

Read More

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

NUnit change order of test run

In version, 3.2 NUnit introduced a new attribute called “Order” to control the order of unit test cases run while running a test suite.

Test cases with Same order number are considered as one, NUnit will decide the order of their run.

The test cases do not have an Order number will follow the regular test run ordering. The Unit tests that have an Order number will always take precedence in Test Run.

Example source code is provided below.
Read More