Unit Testing async MVC Controller methods

14 03 2014

Just had a strange encounter in VS 2013 unit testing. I wrote a unit test for the ASP.NET Identity AccountController, which has some asynchronous methods.
My good old friend Resharper told me to change the method signature to public async void, as I wrote the code to call my controller method using await. The code looked like this:

[TestClass] public class AccountControllerTest {     [TestMethod]
     public async void Register()
     {…
var result = await controller.Register(userModel);

}

}

 

If you run this, it builds well, but nothing happens, even debugging the test is not working. The test is invisible to the test runner!

Strange thing, which, thank good, is not happening too often in Visual Studio.

The only message I got, after fiddling around, was that there are no tests found in my class file.

Even stranger, as my test attributes proof sufficiently, I did all things required to make my test detectable to unit testing, so why is it not carried out???

Did it turn into a phantom? Black magic going on?

No, do not worry, it is not so exciting. As I had to discover, it is the async void keywords!

The background for this is reasonable, because “async void” methods are “fire and forget” ones, which may never come back and this does not really make sense in a test environment, where you need to collect results and assertions from a method. 

What is the correct way to test an aysnc method, then?

You have to use task as return type instead of void, this works well!

[TestClass] public class AccountControllerTest {     [TestMethod]
     public async Task Register()
     {…
var result = await controller.Register(userModel);

}

}

 

Alexander





MVC Web API and ASP.NET Identity – Changing Identity Database Connection

12 03 2014

Sometimes simple things are hard, or at least hard to find.
I have created a Web API and added ASP.NET Identity for user management.

This works well, if one uses the project template, not so well if you want to add ASP.NET Identity at at a later point of time. Therefore just remember it, when creating the project.
However, I did not want to use the preset standard localdb database file  in my application. I was looking where to change the identity database context.
It was obvious that the presets use the “DefaultConnection” connection string in web.config and it works, if you rename the database there, but sometimes I am a bit picky and I wanted to name this special connection differently: “IdentityConnection”.

It really took my a while, because all my searches for DBContext and IdentityDBContext were in vain.
I drilled in deeper and ended up in the Startup.Auth.cs file in the Startup folder of my solution.
Here the user store gets initialized with this line of code:

UserManagerFactory = () 
=> new UserManager<IdentityUser>(new UserStore<IdentityUser>()));

This is the place to change the identity connection string!

One needs to use a different overload of the store’s constructor passing in a new IdentityContext created with the new db connection. Such as this:

UserManagerFactory = () => new UserManager<IdentityUser>
(new UserStore<IdentityUser>(new IdentityDbContext("IdentityConnection")));

And yes, the new “IdentityConnection” connection needs to be configured in Web.Config, of course.

After this, the Identity tables get created in the new database, as specified.

This should not be so hard to find. Maybe the Web API developers have some time to put in a comment in Startup.Auth.cs about db configuration, in the next version.

 

:-)

Alexander





Building State Machines in .NET

22 02 2014

My new online course is out!
If you are creating embedded devices you, of  course, are aware of state machines. They are, or at least should be, an essential infrastructure asset to any solution.
Why?
Well, because state machines build flexibility and extensibility into a system, add structure and are able to scale from device level into backend systems. 
It is one of the most powerful architectural concepts one can use to create solutions that are most enduring and future-proof.

But wait, there is more!
The course adds an event driven communication architecture as well as a service oriented implementation approach to the state machine design, to further remove dependencies and add easy configurability.
Just check it out at my friends at Pluralsight!

Get ready for the Internet of Things and Industry 4.0.

:-)

Alexander





Home Automation Hacking

18 02 2014

although named the “next big thing”, since over twenty years, home automation still is not playing a really important role in our day to day life.
This is quite different, if you look at new professional office buildings, all running on complex automation systems, but somehow the technology never has entered normal housing mainstream.
With a proven track record in this area,  we, at Wechsler Consulting, have decided to start anew at the roots, offering an introductory course leveraging fresh, affordable hardware (Ninja Blocks) that has an easy-to-use user interface as well as simple JavaScript APIs.
With our new course we want to show, how home automation technology can be used to enhance daily comfort and energy efficiency, without getting in the way. There is a lot of potential!

We build on proven professional concepts and architectures that scale well from home into the Internet of Things without sacrificing security and privacy. Get to know the possibilities at  our two day course in Dießen!

:-)

Alexander





New design, new courses! – Wechsler Consulting site re-launched

12 02 2014

I am very proud to announce that we have revamped the complete Wechsler Consulting web site. We did not only take care of the design to make it more responsive and adaptive to a lot of different screen form factors, we also added integration with this blog to provide a one-stop information point and structured the content completely new.

WECON_LOGO_2007_2_centered

But wait there is more!
We have released a significant number of new cross-platform, device development and Cloud trainings and would be delighted if you would take a chance to visit one of them at our location at lake Ammer, one of the nicest places in Bavaria.
Come and find out yourself! :-)

Alexander





A very Happy New 2014!

1 01 2014

to all readers of my blog. :-)

All the best for the New Year…..

Alexander





Great tools for Visual Studio!

12 12 2013

I get quite often asked by customers, which tools to use in combination with Visual Studio. Currently, I do quite a bit of .NET (backend / Azure), HTML5/ Javascript and Xamarin development in VS 2013.

There are two tools I really like and would not want to give them away:

The well known Jetbrains Resharper and, not so well-known, OzCode.
While Resharper helps me organizing and optimizing code in C# and HTML 5, OzCode speeds up debugging in C# significantly (no more Quickwatch).

Both have trial versions out! :-)

Alexander








Follow

Get every new post delivered to your Inbox.