Category Archives

9 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

Update Asp net core 2 Angular SPA template to work with Angular 5

Update Asp net core 2 Angular SPA template to work with Angular 5

The source code of this project is added into GitHub, you can download, clone or submit your changes to this repository.
Link to GitHub repository
https://github.com/shibut12/ng5-netcore

The project is added to Travis CI to auto build.
Tavis ci build status

My system configuration

  • @angular/cli: version 1.5.5
  • Node: Version 6.11.0
  • npm: Version 3.10.10
  • dotnet: Version 2.0.2

First things first

Before you begin creating a project and updating, let’s look into preparing the environment before we create the project. Run following commands in Command Prompt or shell.

mkdir ng-netcore
cd ng5-netcore
npm uninstall -g @angular/cli
npm cache clean
npm install -g @angular/cli@latest
npm install -g npm-check-updates

Read More

Dotnet core 2 Angular 5 : Error “app.browser.module.ngfactory” while publishing

Dotnet core 2 Angular 5 : Error “app.browser.module.ngfactory” while publishing

This article talks about ” Can’t resolve ‘./../$$_gendir/ClientApp/app/app.browser.module.ngfactory'” error while publishing a dot net core 3, angular 5 application created from SPA template and how to fix it.
When I was building my aspnet core web applciation, I got following errors in my Gitlab CI pipeline, in this article, we will try to resolve them.

  • Module not found : error : Can’t resolve ‘./../$$_gendir/ClientApp/app/app.browser.module.ngfactory’
  • error MSB3073: The command “node node_modules/webpack/bin/webpack.js –env.prod” exited with code 2.

My setup

@angular/cli - Version 1.5.5
Node: Version 8.2.1
npm: Version 5.5.1
dotnet code: Version 2.1.1-preview-007165

Step 1: Create a new Dotnet core 2 Angular application from SPA

Run following commands in a shell

mkdir ng5-app
cd ng5-app
dotnet new angular
npm install

Step 2. Verify the angular version and upgrade to angular 5

At the command prompt, run following command to install a utility to check outdated packages (Note: to run these commands, you should have npm installrun as explained in step 1).
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

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

Uninstall Dot net core Preview 2 in Ubuntu 16.04 LTS

Uninstall Dot net core Preview 2 in Ubuntu 16.04 LTS

Though the instructions at Microsoft website is really easy to follow to install Dotnet core 2.0, I realized that the web page does not have instructions that can be easily followed to remove Dotnet core preview 2.

https://www.microsoft.com/net/core#linuxubuntu

Run the following commands to removed Dotnet core 2.

sudo apt --installed list

sudo apt --installed list | grep "dotnet-sdk"

sudo apt-get remove dotnet-sdk-2.0.0-preview2-006497

dotnet --version

 

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

The type or namespace name system could not be found Error in C#

How to resolve “The type or namespace name system could not be found” and “Predefined type system object is not defined or imported” errors in a .Net project c#?

Install the latest version of System.Runtime Nuget package or add a reference to System.Runtime from “Add references”.

Clean and rebuild the project once Reference is added.

PM> Install-Package System.Runtime


Read More