Creating ASP.NET Core MVC Integration Tests – Getting Started

Tests should be created to make your life easier down the line. When you may or my not have to add new functionality or rework some previous code. However Unit tests may not always be enough, so we end up using integration testing as well.

Prerequisites :

*  You might run into some issues when trying to add the necessary using statements. Its a known issue (https://github.com/NuGet/Home/issues/4412).

In this post I will take about creating a test to check if a particular form is properly rendering as HTML.

Here is what your test controller should look like.

public class LoanFormShould
{
[Fact]
public async Task RenderApplicationForm()
{
// Creating a web Host Builder to put in your test server
var builder = new WebHostBuilder()
.UseContentRoot(@"Enter your Path here to project")
.UseEnvironment("Development")
.UseStartup<loans.Startup>()
.UseApplicationInsights(); // Only need this if your using that feature
var server = new TestServer(builder);
var client = server.CreateClient(); // creating a client to make a request
var response = await client.GetAsync("/Apply");
response.EnsureSuccessStatusCode();
var responseString = await response.Content.ReadAsStringAsync(); // Getting the string of HTML
Assert.Contains("New Loan Application", responseString); // Using the Assert Contains to see if the has the right title
}
}

You maybe asking at this point what about our database how will we actually be testing if our application performs CRUD operations properly ? Well for that we are going to being the In-memory database provider for Entity Framework. So that we never actually end up touching our actual DBs.

The only change you have to make once you have added the package is checking if you in development or not in your Startup.cs file. As seen below:

public void ConfigureServices(IServiceCollection services)
{
if (CurrentEnvironment.IsDevelopment()) // Here is where we check
{
services.AddDbContext<AppDbContext>(
options => options.UseInMemoryDatabase()); // And we ask it to use the InMemoryDatabase Option
}
else
{
services.AddDbContext<AppDbContext>(
options => options.UseSqlServer(
Configuration.GetConnectionString("DefaultConnection")));
}
services.AddScoped<ICreditCardApplicationRepository,
EntityFrameworkCreditCardApplicationRepository>();
services.AddMvc();
}

view raw
InMemoryDBTesting.cs
hosted with ❤ by GitHub

And that is it. Now your ready to start integration testing in .NET Core MVC 🙂

Understanding Unity – Part 1

Introduction:

Unity is one of the most popular and easily accessible game engines to date. Which has lead it to become of the best documented and beginner friendly games engines. It can be used to create both 2D and 3D games, and uses C# as it’s main scripting language. And the best part is that every part of it is completely free. Combined with its robust asset store and plethora of free online tutorials it is the game engine to learn these days.

How Unity Works:

To understand Unity, you need to first understand the differences between a library and a engine. A library is something to that you use to build your application, whereas a engine is something that only calls your code.

Unity is an engine, it is something that you have to understand and use in your pursuit to make a game. You do not get to decide on how the physics engine works or how the lighting effects work. You are just given a number of ways to use the sub systems that were created for you.

When you create anything in unity your making a object. Every object in unity has certain properties attached to it, like its position and scale. You can add other components to it like a particle emitter or sprite render. The way you start coding your game is by way of using scripts that you attach to the object like any other component. Once the script is attached you can then start manipulating the different attributes your game object has.

In the second part of this series I am going to talk about the life cycle of a unity object.

Getting Started with Express.js

Intro

Raise your hand if you already know the MVC design pattern ! … oh right you can’t.

You can look at one of my previous blog posts here. Now that we have gotten that out of the way. Express.js provides an incredibly simplistic and transparent way to develop APIs for your applications. It’s one of the most popular frameworks to run on the Node.js platform.

You simply define your routes, define what you want to send out and pull in, and your golden 👍

Setting it up:

var express = require('express')
var app = express()

view raw
settingUp.js
hosted with ❤ by GitHub

Routes:

app.get('/', function (req, res) {
res.send('hello world')
})

view raw
routes.js
hosted with ❤ by GitHub

Here you can see the route in question is just a slash. That means that when ever anyone hits the https://”your domain”/, this route gets triggered (since it is a GET request). Once the route gets triggered it executes the function, that has the “req”, and “res” variables. The “req” is your request object, and the “res” is your response object. You can put different things in the res object and it gets passed along to be rendered in HTML.

This same sort of logic can be applied to POST and PUT requests.

Folder Structures:

You can use a variety of different folder structures. What ever folder structure you use, make sure its suited for the type of app your going to be building. You can checkout an example over here.

And your done ! Easy as that. You may be thinking WTF is that really it ? Why do people want to use Express.js if this is it ? Well the answer is that the frame work does not lock you into, using any specific way of doing things. Therefor you can use any sort of authentication, object relational mapper, session manager, or data base you want.

You can find the best 40 packages most commonly used here.

Going from Apple fanboy to hater to fanboy again

When I was kid just going to primary school, I never knew what a computer was, how it worked, or what it could do. My family had a computer at home but it was just a big grey box to me. It just sat on a desk.

However on my first day of school we had time with computers, we had this really great computer lab full of macs, and we had carts full of mac laptops. They looked cool, and you could do a lot with them. Even when being a 6 year old kid, I could appreciate the design and work that went into them. It felt like someone put in a lot of time to just make sure everything was perfect.

So after using them for awhile, I asked my dad if we could get one. I went into full sales pitch mode, and told him about all the cool things that it could do, and how well it was designed. However being new immigrates to Canada, that computer was out of our price range, and he said to just use the one we already had. My dads response was, “Why don’t you use the computer we already have ?”. And that computer was a Comqa running Windows 95. The experience of using that machine made me into an even bigger Apple fan.

As the years passed and I got to high school I still hadn’t owned an apple product, and started to rationalize why I didn’t have an apple product. I chalked it up to the good old Marxist ideas of the “haves” and the “have nots”. I was a “have not”. So my views on Apple changed from a being “Its such a cool company that makes awesome products” to “They make play things for idiots that choose to buy it”

That ideology lasted for a pretty long time, till I started making money. A year after my first internship I had quite a bit cash saved up even after paying for university. So in my second to last year (or what I had thought to be my second to last year), I called my bank, raised my credit limit, and bought a bueatiful 2016 13′ MacBook Pro. And I was a fan again 🙂, to long had I been using a windows machine.

What most people don’t realize is that Apple really cares about two things, build quality, and the little things. That laptop felt like a heavy solid chunk of metal, that just happened to be a laptop. The build quality was insanely good.

When a person pays a crazy sum of money for a fairly basic computer, most people are would say it’s dumb to do so. Others would say maybe he or she is just that rich. Most people equate value with quantity. However in Apples case, it’s build quality, and effort put into places you wouldn’t even think of.

And that right there is what Apple is unapologetically all about.