Unit Testing – Setup pattern (Moq)

Frank Code

In my previous post I mentioned that large unit test setups can be difficult to maintain / understand. This problem can be reduced by following a test pattern. I want to share with you a pattern I use, and I think works really well. In this post I will be using C#, Moq libary and Visual Studio test tools.

To start here are some things I believe make a good suite of unit tests:

  • Easy to identify data dependencies of tests.
  • Mocking is not repeated every test.
  • Test are clear and easy to understand / maintain.
  • New tests can be added with relative ease.

In order to explain this testing pattern I have forked my buddies TodoMVC app, and introduced a service layer, repository pattern and test project. You can see my source code with an example test class on github.

The TodoModule (service layer) has a number of public methods; Get…

View original post 315 more words

Object Oriented, Test Driven Design in C# and Java: A Practical Example Part #1

Download the code in C#

For a brief overview, please refer to this post.

At this point, many tutorials start by launching into a “Hello, World” style tutorial, with very little practical basis.

HelloWorld

This isn’t the most exciting concept, so let’s try a more practical example. Instead of churning out boring pleasantries, our application is going to do something a bit more interesting…build robots.

Robot

Specifically, our application is going to build awesome robots with big guns.

Ok, let’s get started with a narrative description of what we’re going to do.

“Mechs with Big Guns” is a factory that produces large, robotic vehicles designed to shoot other large, robotic vehicles. Robots are composed of several robotic parts, delivered by suppliers. Parts are loaded into a delivery bay, and are transported by worker drones to various rooms; functional parts such as arms, legs, etc., are dispatched to an assembly room. Guns…

View original post 876 more words

WCF Policy – Part 8: Making the Information Available on the Client

AJ's blog

Our policy has its default behavior. All the information is collected, it just is not available to the developer yet. So let’s take care of that.

We already have the basics covered, namely we have an interface ITrackingInformation through which the application developer should have access to the information. All we need to do is to build a bridge between message inspectors and the application code.

On the client the application developer uses the generated client proxy class, thus we need to pass in the tracking information to that object. The class derives from ClientBase<TChannel> and thus inherits the property Endpointof type ServiceEndpoint. This class in turn maintains a collection of endpoint behaviors. With a custom behavior which implements our interface, the developer has the gate to our policy. Going one step further, this behavior can also register a message inspector in IEndpointBehavior.ApplyClientBehavior, which utilizes…

View original post 321 more words

SOLID principle

Chanmingman's Blog

There is a lot of debate on SOLID principle.

The video Applying SOLID Principles in .NET   (http://channel9.msdn.com/events/TechEd/Europe/2014/DEV-B210) is worth to watch.

I like the last slide Consideration

SOLID

As he mentioned.

1. Someone just over engineering using the term SOLID

2. How readable for your Dependency Injection if you have large set of object.

3. When talking about maintainable, before SOLID is 50 lines of code, after SOLID is 230 lines of code previewing the same functionality. So you think 50 lines of code is easier to maintain or 230?

View original post

Clean event handler invocation with C# 6

Jon Skeet's coding blog

The problem

Invoking event handlers in C# has always been a bit of a pain, because an event with no subscribers is usually represented as a null reference. This leads to code like this:

It’s important to use the handler local variable, as if instead you access the field twice, it’s possible that the last subscriber will unsubscribe between the check and the invocation:

Now this can be simplified slightly using an extension method:

Then in each event, you can write a single line:

However, this means having a different extension method for each delegate type. It’s not too bad if you’re using EventHandler but it’s still not ideal.

C# 6 to the rescue!

The null-conditional operator (?.) in C# 6 isn’t just for properties. It can also be used for method calls. The compiler does the right thing (evaluating the expression only once) so you can do…

View original post 407 more words

Essential .NET Development Tools & More

Here is (mostly) comprehensive list of tools for development.  It’s like Scott Hanselman’s, but focused almost purely on development, with a couple of extras.  While you’re at it, go check his out.  All opinions are my own and are not bought or sold.

The Main Stuff

Visual Studio – king of IDEs and the essential tool for .NET devs everywhere. Not much else to say except that it has a great starting toolset for any developer and amazing plugin support.  The Community edition, new as of a couple of months ago, gives the masses the power of VS Professional, for free.  Simply amazing and getting better with every release.

SQL Server Management Studio – it ranges from a useful IDE for SQL to a huge time saver for things like table creation and script generation.  The DROP and CREATE tools are awesome for generating scripts for tables, stored procs and more, and the Execution Plan viewer can be very helpful when trying to optimize expensive queries.  The Express edition has most of the tools required to manage a day-to-day SQL Server installation just fine.  One of the best database management tools available.

SQL Server Profiler – for when I’m having a hard time figuring out exactly how well a SQL query is running or when the SQL that’s being emitted from Entity Framework is not immediately known to me, I always turn to Profiler.  Used it to find a show-stopping SQL query on more than one occasion.

LINQPad – write LINQ queries using the best .NET code scratchpad on the market.  It’s not a complete replacement for SQL Management Studio, but for complex queries with lots of data, it’s my first choice.  The Premium edition is a steal and makes this essential tool 5 times more useful with C# autocomplete, NuGet, and cross-database query support.  Plus, it’s great for those one-off tests for code behaviors that you might need a quick refresher on. LINQPad’s author, Joe Albihari, is always adding new features – most recently an integrated debugger.

NimbleText – thanks to Scott Hanselman, I have found this program – and my new favorite way to write repetitive code or handle small or large data transformation tasks.  I’ve used it from everything from writing HTML to generating SQL insert scripts.  Its time-saving power cannot be understated.  And, it’s FREE!

Notepad++ – my text editor of choice.  Decent plugin support, syntax highlighting, and instant right-click, edit for any file in Windows Explorer.  (Yeah, I know dev-favorite Sublime Text has these things too, but Notepad++ was my first and it fits my needs nicely.)

SourceTree – an essential tool for Git users on Windows.  I just started using Git (yeah, I know I’m late to the party) and SourceTree has made the transition from TFS to Git that much more smooth.  Not perfect, but very helpful.

dotPeek – my favorite way to decompile .NET code, free from JetBrains.  It even has the ability to break a .NET DLL/EXE down into a fully-structured Visual Studio project!  (I actually have a client whose previous developer wouldn’t hand over the source code for their main application, and dotPeek made the decompile way more convenient.)

Postman (Chrome extension) – my second-favorite way to test HTTP services is Postman.  Postman has an easy-to-use interface and provides a straightforward way to make HTTP requests.

Continue reading “Essential .NET Development Tools & More”

Marqueed – Online Collaboration (FREE)

Marqueed is a free way to markup and discuss images online. You can easily add multiple images with the drag and drop interface, either from your computer or another browser window. Adding collaborators is simple via email.

Collaborators can point to areas and have threaded discussions right on the image. There’s even a freehand drawing tool for more options.

marqueed

Blog at WordPress.com.

Up ↑