Skip to main content


Showing posts from July, 2009

Jquery for Select All Checkbox

A very common interface is a list of items with a checkbox to select each and them you perform an action on those selected. Typically you have a checkbox at the top to toggle all of the list. This script will take care of everything for you. You can exclude elements by checking the ID or name properties.$(document).ready(function() {
$("#selectAllCheckBox").click(function() {
var checked_status = this.checked;
$("input[type=CheckBox]").each(function() {
if ( != "pleaseSkipMe") {
this.checked = checked_status;

Ajax Call Issue

When making an Ajax call, remember to make sure that your target element can accept the result of your call. For example, if I return a string from my call to a target element of <p>, everything is fine. Replace that with a table and you’ll get the lovely message “htmlfile: Unknown runtime error”. Change to a <div> and everything is fine.

Asp.Net MVC Unit Testing Authorization

Asp.Net provides the Authorize attribute for checking to make sure you have an authenticated user and that they are in the correct role(s). Since this is part of the framework, I don’t need to test the attribute (that’s Microsoft’s job). I do want to test that I have applied the attribute with the correct properties.Here’s my unit test code for checking that the attribute has been applied.[TestMethod()] publicvoid Autorization_Attributes_Have_Been_Applied() { MethodInfo[] methodInfos = typeof(CustomersController).GetMethods(BindingFlags.Public | BindingFlags.Static); foreach (MethodInfo methodInfo in methodInfos) { ParameterInfo[] parmInfo = methodInfo.GetParameters(); if (methodInfo.Name == "Create" | methodInfo.Name == "Edit") { var attributes = methodInfo.GetCustomAttributes(typeof(AuthorizeAttribute), true); Assert.IsNotNull(attributes); Assert.AreEqual(1, attributes.Length); var authorizeAttribute = (AuthorizeAttribute)attributes[0]; Assert.IsTrue(authorizeA…

UpdateModel Unit Testing Gotcha!

If you’re unit testing and make a call to UpdateModel, you get the case where your object doesn’t get updated.Say your controller has an Edit Post action similar to:[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Edit(Customer customer)
Customer _customer = repository.GetByKey(customer.CustomerKey);
return RedirectToAction("Index");
} Now you might think that the call to UpdateModel will use your the object you passed in. Wrong! It wants the form values that get passed as part of the post request. So if you’re unit testing and using MVC fakes, you need to pass in some form values. Customer customer = new Customer()
CustomerKey = 1234,
CustomerName = "ABC Siding",
Address = "1 Main",
City = "Springfield",
State = "IL",
ZipCode = "62701",
FirstName = "Fred",
LastName = "Jones",
Phone = "2175551212",
Fax = "217555…

Fake HttpResponse for Asp.Net MVC

Here’s an HttpResponse fake to go with Stephen Walther’s MVC Fakes. It’s based on his HttpRequest. If you need something for testing the cache, look here.publicclass FakeHttpResponse
: HttpResponseBase
private HttpCookieCollection _cookies;
private NameValueCollection _headers;
privatebool isClientConnected;
privatebool isRequestBeingRedirected;
private TextWriter output;
private Stream outputStream;

public FakeHttpResponse(
NameValueCollection headers,
HttpCookieCollection cookies
this._headers = headers;
this._cookies = cookies;

public FakeHttpResponse(
NameValueCollection headers,
HttpCookieCollection cookies,
bool isClientConnected,
bool isRequestBeingRedirected,
TextWriter output,
Stream outputStream
this._headers = headers;
this._cookies = cookies;
this.isClientConnected = isClientConnected;
this.isRequestBeingRedirected = isRequestBeingRedirected;
this.output = output;
this.outputStream = outputStream;


Unit Testing MVC and the Cache

If you haven’t worked with Stephen Walthers MVC Tip #12, you’re missing out on a great way to test your MVC controllers. He provides fakes for the ControllerContext, HttpContext, HttpRequest, SessionState and also mechanisms for testing authorization. You can download his code at the end of the post.I created a new project called MvcFakesForTesting and copied all of the classes. I did this so I can extend and grow the project. Stephen wrote his library with pre-release code, so I needed to change a couple of IController references to ControllerBase. I also created added Headers so that I can fake the request headers. Just implement this the same as QueryString and FormParams. Now I have the DLL that I will use on all of our projects for testing. I made sure to include a reference to Stephen so that credit is given where credit is due.One thing that is not included is a fake for the Cache. Of course, I ran into this on my first test! First of all, the Cache object is sealed / notinheri…

KVM Switch gives USB not recognized

My TrendNet TK409K KVM popped up with a “USB not recognized or malfunctioned” today. The solution I found was to unplug all of the USB connections for a minute. This apparently “resets” the box. Plug everything back in and away you go.

T-SQL to create an instance of an object with the properties set

Here’s some T-SQL to create a SELECT statement that will create the properties with values. I use this for generating test data from existing DB’s. Right now I’ve got it setup to handle int, char, varchar, smalldatetime, datetime and bit. I’ll add others as needed.@TableName is the name of the table where the data is stored. Simply set the table name hit F5 then cut and paste the generated script into the T-SQL that gets the data your looking for. This can be all rows in the table or a single row.The code:declare @TableName varchar(100)
declare @ColumnName varchar(100)
declare @DataType varchar(100)
declare @Sqlvarchar(max)

declare @Crlf char(2)

set @TableName = 'Clients'
set @Sql = CHAR(39)

declare cols_temp cursorfor
and data_type in ('int', 'char', 'varchar', 'smalldatetime', 'datetime', 'bit')

open cols_temp

FETCHNEXTFROM cols_temp INTO @ColumnNa…

What’s in a name?

I’ve worked on database projects for over 20 years. I go back to FoxBASE and dBase. I’ve also been through a number of naming conventions in that time. I’ve developed a personal preference but can work with other preferences because I can usually figure them out fairly quickly. Here’s two rules I use as part of my database designs:Use a surrogate or artificial key as the primary key to avoid the need for cascading deletes or updates. Use the natural name for each element so that elements are easily identifiable when in discussions with the user.I can’t tell you how much time I have saved over the years with rule 2. Users have no idea of your database design (and they shouldn’t!) but they know exactly which piece of information they want. It’s a whole lot easier if they can write “client’s birth date” in a report request and in the Clients table there is a field called BirthDate.My problem is lately, I’ve noticed that a lot of people seem to be using a convention that drives me nuts - …