It’s good to touch the green green grass of Test Explorer in LightSwitch (part 1/n)

Introduction

I have done in the past some attempts to activate unit testing of the server side code for a LightSwitch project.

It were all nice trials and indeed each trial was a bit better than the previous one.

This time, I’m determined: when you want unit tests, continue reading…

 

The Sample Project

In order to make things as visible as possible we presume that we want to test following simple business rule.

We have a project with a customer table with 3 fields:

Customer

 

The field ServerCalculatedValue is, well euh… calculated server side:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.LightSwitch;
using Microsoft.LightSwitch.Security.Server;
namespace LightSwitchApplication
{
    public partial class ApplicationDataService
    {
        partial void Customers_Inserting(Customer entity)
        {
            entity.ServerCalculatedValue = "Server Calculated for " + entity.LastName;
        }
        partial void Customers_Updating(Customer entity)
        {
            entity.ServerCalculatedValue = "Server Calculated for " + entity.LastName;
        }
    }
}

We want to test now this simple business rule.

 

So, add first a unit test project to the LightSwitch solution:

 

unittestproject

 

 

 

In the unit test project add a reference to the LightSwitch server project:

serverprojectReference

 

 

as well as to

 

  • C:\Program Files (x86)\Microsoft SDKs\LightSwitch\v4.5\Client\Microsoft.LightSwitch.dll
  • C:\Program Files (x86)\Microsoft SDKs\LightSwitch\v4.5\Server\Microsoft.LightSwitch.Server.dll

refs

 

 

 

 

 

 

 

Change one option in visual studio

option

So, change the web application root directory to the folder where you store all your visual studio projects.

In my case that f:\WS.

WS stands for workspace, and it’s folder pointing to the root of my TFS.

The folder of the current solution I’m working on is: F:\WS\html5\LSUnitTesting

So, LSUnitTesting is the folder of the solution.

It will become clear in a minute why we need to change this option. It’s nothing LightSwitch specific.

Add a Test.aspx form to the Server project

Sure, you know how to do this:

testaspx

The Test.aspx file does not need any specific content. It just need to be there.

The unit test class

Looks as follows:

using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.VisualStudio.TestTools.UnitTesting.Web;
using LightSwitchApplication;
using System.Linq;
using Microsoft.LightSwitch;
namespace UnitTests
{
    [TestClass]
    public class UnitTest1
    {
        [TestMethod]
        [HostType("ASP.NET")]
        [UrlToTest("http://localhost:4151/Test.aspx")]
        [AspNetDevelopmentServerHost(@"%PathToWebRoot%\Html5\LSUnitTesting\LSUnitTesting\bin\Debug")]


        public void TestMethod1()
        {
            using (ServerApplicationContext ctx = ServerApplicationContext.CreateContext())
            {
                Customer c = ctx.DataWorkspace.ApplicationData.Customers.AddNew();
                c.LastName = "van bladel";
                string guidString = Guid.NewGuid().ToString();
                c.FirstName = "paul" + guidString ;
                ctx.DataWorkspace.ApplicationData.SaveChanges();

                string expectedValueForServerCalculatedValue = "Server Calculated for " + c.LastName;

                var retrievedCustomer = ctx.DataWorkspace.ApplicationData.Customers.Where(cus => cus.FirstName == "paul" + guidString).Single();

                string actualValue = retrievedCustomer.ServerCalculatedValue;

                Assert.AreEqual(expectedValueForServerCalculatedValue, actualValue);
            }
        }
    }
}

The Test itself is quite trivial, we want to test our server side business logic rule about the field ServerCalculatedValue.

So, we create a ServerApplicationContext, we create a a customer and we verify if the server correctly updated the field ServerCalculatedValue.

Where is the magic?

There is no magic, there are only the 3 classic attributes :

  • HostType[ASP.NET]
  • [UrlToTest("http://localhost:4151/Test.aspx")] : This points directly to the dummy aspx page we created earlier. Note also that the port needs to point to the port used by IIS Express.
  • [AspNetDevelopmentServerHost(@"%PathToWebRoot%\Html5\LSUnitTesting\LSUnitTesting\bin\Debug")]the environment variable %PathToWebRoot% points nicely to the visual studio option we changed above. It’s not mandatory to use this environment variable, but it just makes life easier.

Let’s run the test

 

runtest

 

 

As you notice, it’s good to touch the green green grass of Test Explorer.

cheers

Paul.