Wednesday, 31 March 2010

Testing Private Methods in .Net

Some developers seem to be against testing private methods, that if you need to test private methods there is a design issue. I don't think I agree in all cases and extracting some method logic into another method for further testing seems valid to me. Having to make that method public (against it's design) just for testing purposes doesn't sit right, so I test private methods using the PrivateObject class found in the Visual Studio TestTools library...

[TestMethod]
public void IsValidInput_NumberString_ReturnsTrue()
{
 // Arrange 
 var myClass = new MyClassWithPrivateMethod();
 var privateObject = new PrivateObject(myClass);

 // Act
 bool result = (bool)privateObject.Invoke("IsValidInput", new object[] { "123" });

 // Assert
 Assert.IsTrue(result);
}

Powershell V2 on Windows XP x64 OS

Just had a mild heart attack... I couldn't find a download for Powershell V2 for Windows XP x64 OS! But turns out XP x64 is half XP - half Server 2003! So I've downloaded the Windows Server 2003 x64 install and it works fine!

Panic over! :)

DRY CSS == .LESS

Lots of talk regarding the .LESS (pronounced dot-less) library has prompted me to finally get around to incorporating it within one of my projects - and I'm impressed. .LESS extends on CSS, making your CSS dynamic. It allows you to work with your CSS more easily and without needing to repeat yourself by way of 4 powerful language enhancements:

Variables

Variables specify widely used values in one place such as site colours.

@companyRed: #eeeeee;

h1, h2   { color: @companyRed; } 
#banner  { color: @companyRed; }

Mixins

Collecting sets of rules within a class and allowing other rules to add that class as a property.

.myMixinClass { 
   color: #ffffff; 
   text-align: center; 
   font-weight: bold; 
}

#banner   { .myMixinClass; padding: 30px; }
#footer   { .myMixinClass; padding: 0px; }

Operations

Perform simple math operations between related properties.

@defaultBorder: 1px;
@companyRed: #eeeeee;

#banner {
  color: @companyRed;
  border-left: @defaultBorder;
  border-right: @defaultBorder * 2;
}

#footer { 
  color: (@companyRed + #111) * 1.5;
}

Nested Rules

Avoid long selector names for specifying inheritance, allowing you simply to nest the rules making the CSS a lot more readable.

#header {
  color: red;
  a {
    font-weight: bold;
    text-decoration: none;
  }
}

How does .LESS work?

These language enhancements are parsed and converted to valid CSS on the fly, or can be pre-compiled. It's amazingly easily to start working with and the homepage has all the info you'll probably ever need on .LESS - it really is that simple :)

Performance?

.LESS removes comments by default and has built in support for minification and caching via the optional config values. Although if you are serious about performance I'd suggest using .LESS to compile your .LESS files to .css during the build and leave the caching and compression to IIS.

Gotchas to avoid...

  • Visual Studio 2008 won't know what type of file a .LESS file is, which means it opens it as a plain text file. There is a .LESS Visual Studio Integration Pack (if it asks you to login then click 'login as guest') that I recommend you getting so you have syntax highlighting at least - but I'm afraid intellisense won't be able to help you here.
  • Remember to make sure IIS is setup to forward .LESS resource requests to the ASP.Net engine (if you are working on MVC apps then it will by default).
  • Visual Studio won't set a build action on .LESS files, so remember to set build action to 'content' or the files won't be deployed.
  • As .LESS parses the file I noticed it fell over (didn't process the rest of the CSS file) on this particular CSS hack "border:none!important;" so you may need to rethink your hacks if .LESS doesn't like it.

Monday, 29 March 2010

Visual Studio Team Foundation Server Power Tools

I often forget to comment my check-ins and I'm getting fed up with having a big sign stuck to my monitor saying "Comment Your Check-Ins!". So I've decided to make use of Team Foundation Server Power Tools to enforce a custom check-in policy to ensure everyone checks in comments with their changesets.

Team Foundation Server Power Tools is a set of enhancements, tools and command-line utilities that improve the Team Foundation Server (TFS) user experience.

Other useful features of the add-on include the ability to enforce a 'testing policy' to ensure tests are run before check-in and an alert editor for enabling subscriptions to events (e.g. failed builds) in Team Foundation Server. A very useful add-on indeed :)

Thursday, 25 March 2010

How to Check if ASP.Net Debugger is Attached

Want to know how to check if the ASP.Net debugger is attached in code?

System.Diagnostics.Debugger.IsAttached

Visual Studio Build Actions Property

Each file within a .Net project has a build action property for specifying the build action on the file when a build is executed. Typically the buld action will be one of these:

  • None - The file is not included in the project output group and is not compiled in the build process. An example is a text file that contains documentation, such as a Readme file.
  • Compile - The file is compiled into the build output. This setting is used for code files.
  • Content - The file is not compiled, but is included in the Content output group. For example, this setting is the default value for an .htm or other kind of Web file.
  • Embedded Resource - This file is embedded in the main project build output as a DLL or executable. It is typically used for resource files.

However, this is not the definitive list as the build action property is extensible.

The default build action property set by Visual Studio is set according to the extension of the file that you add to the solution.

e.g. a Class.cs file would get a build action of compile as it's a code file - simples :)

Thursday, 4 March 2010

How to Zip and UnZip Using Powershell

Here are some easy-to-use functions for zipping up a directory and unzipping using Powershell. The functions use the opensource SharpZipLib library, a Zip, GZip, Tar and BZip2 library written entirely in C# for the .NET platform. So you'll need to download this and reference the SharpZipLib assembly in the appropriate place in the functions below.

function ZipFiles($sourcePath,$zipFilename)
{
   # Reference the SharpZipLib assembly here
   [System.Reflection.Assembly]::LoadFrom("C:\ICSharpCode.SharpZipLib.dll")

   $zip = New-Object ICSharpCode.SharpZipLib.Zip.FastZip
   $zip.CreateZip($zipFilename,$sourcePath, $true,"")
} 


function UnzipFiles($zipFilename,$destinationPath)
{ 
   # Reference the SharpZipLib assembly here
   [System.Reflection.Assembly]::LoadFrom("C:\ICSharpCode.SharpZipLib.dll")

   $zip = New-Object ICSharpCode.SharpZipLib.Zip.FastZip
   $zip.ExtractZip($zipFilename,$destinationPath,"")
} 

Powershell and Where to Begin...

Go to the Powershell website and download PowerShell. It isn't that obvious but it's part of the Windows Management Framework download. First time you run scripts you'll need to remove the restricted execution policy by running Powershell from the command line and running the command:

set-executionpolicy remotesigned

There is a bundled editor in the Powershell download but I use PowerShell GUI, it's quite a nice editor and has intellisense :)

jQuery .html() Problems in Internet Explorer

So I've developed my web application in firefox, all seems to be working fine but before I check-in I quickly check the application in the other major browsers; they all work great. All work great APART from IE!

In IE it turns out that the jQuery .html() attribute won't replace contents of first matched element unless the new HTML is valid (i.e. opening and closing tags match). Guess I can't complain with IE not liking the HTML I'm giving it when its imperfect. If only IE practiced what it preached! :)

// This fails in IE...
$('#someDivID').html("<div id='newDivID'><p>hello world</p></div></div>");

// This works in IE...
$('#someDivID').html("

hello world

");

That is all.