Tuesday, September 30, 2014

Unit Test Template for VS2013

Problem
Starting in VS2012 Microsoft removed the Generate Unit Tests option from the context menu, because it was just too efficient. With good intentions these people were kind enough to create an almost replacement, here: http://visualstudiogallery.msdn.microsoft.com/45208924-e7b0-45df-8cff-165b505a38d7. Too bad it only works on public methods (in other words, it doesn't work with internal methods!).

Solution
Fine, i'll just save this unit test template to the testing project, and copy and paste every time i want to test a method. At least Microsoft won't be able to take this away from me.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace UnitTestingProjectName
{
    [TestClass()]
    public class UnitTestClassName
    {
        [TestMethod()]
        public void UnitTestMethodName()
        {
            //Arrange

            //Act

            //Assert
        }
    }
}

"Create Unit Test..." is missing in VS2013

Problem
The Create Unit Test option in the context menu is missing in Visual Studio 2013.

Microsoft removed that because it was tightly coupled to their unit testing product, and they wanted to open up VS to external unit testing products.

Solution

1. Tools > Extensions and Updates

2. Click Online

3. Search for Unit Test Generator

4. Click on it, download it, and install it

warning: This currently only works for public methods, so it doesn't help for internals. How I'm using it generating a template, and then modifying it so it works with the internal class.

Reference
http://channel9.msdn.com/posts/Introducing-the-Unit-Test-Generator-for-Visual-Studio

Monday, September 22, 2014

SQL Alias doesn't work if network disconnects

Problem
I'm using a named pipe alias, and when the network disconnects I'm no longer able to connect using the alias.

My settings are as follows:
Alias Name: SERVER_NAME\INSTANCE_NAME
Protocol: Named Pipes.
PipeName: \\MY_COMPUTER_NAME\pipe\MSSQL$MY_INSTANCE\sql\query
server: MY_COMPUTER_NAME

Solution
Change the alias protocol to TCP/IP and specify the server\instance name in the Server textbox, as follows:
Alias Name: SERVER_NAME\INSTANCE_NAME
Protocol: TCP\IP
server: MY_COMPUTER_NAME\INSTANCE_NAME
Confirm this is working by disabling / disconnecting from your network and trying to connect to SQL using the alias.

Reference
I only found one other post that had the same problem as me. I answered their question on stacked overflow since it worked for me. http://stackoverflow.com/questions/10466700/local-sqlserver-with-alias-doesnt-respond-if-disconnect-from-lan-internet/25981672#25981672

Monday, September 15, 2014

Tuesday, September 2, 2014

Dexterity - modified Transaction template for when you require a transaction

This is from the Microsoft Dexterity Help 12.0b2 in the Transaction template page. I have modified it so that it only proceeds with the logic if it can start a transaction. 

A good, clean design has error handling separated from the business logic, so it calls DoLogic() which does the actual business logic. Errors bubble up to this error handler. This keeps the transaction / error handling away from the logic and really clarifies the business logic as well as the error handling.

 

{The Transaction Template}
local integer retry_count;
local integer MAXRETRIES = 3;
local long sleeptime;

retry_count = 0;
try
    if CanStartTransaction() then
        transaction begin;
  if DoLogic() then
                 transaction commit;
  else
   transaction rollback;
  end if;
 else
  warning "DoLogic() must be executed within a transaction, but one cannot be started."
    end if;

catch [EXCEPTION_CLASS_DB_DEADLOCK]
 
     if retry_count < MAXRETRIES then
         sleeptime = Timer_Sleep(10);
         increment retry_count;
         restart try;
     else
         throw;
     end if;
    
else
    transaction rollback;
    error "An exception occurred. Class: " + str(Exception_Class()) + " Subclass: " + str(Exception_SubClass()) + " Message:" + Exception_Message();
end try;
There was an error in this gadget