In my previous articles, we setup SoapUI and Postman to test the responses to our submissions. While this is an important testing method, it is time consuming and can be difficult to test all the various scenarios. This where Unit Testing comes in. With Unit Tests we can test our code for regressions in seconds (or milliseconds) and as we write our code.
Unfortunately, the design we have followed so far does not support Unit Testing, so we will have to make some changes. The best practice is to write your test, then write your code (make your code changes). So that is what we are going to do.
Add a Unit Test Project
Ok. Let’s get started.
- Launch Visual Studio.
- Open our ScriptLink demo solution.
- Right-click on the solution and select Add->New Project.
- Set the Language filter to C# and the Project Type filter to Test.
- Select Unit Test (.NET Framework) and select Next.

- Name the project.
Common naming patterns use the name of the project you are testing appended with “.Tests” Mine will be RS.ScriptLinkDemo.Soap.Tests. - Select Create.

You should now have a Unit Test project added to your solution with a sample Unit Test (UnitTest1.cs) open and ready to edit. Go ahead and delete UnitTest1.cs. We will be walking through the creation of each test.

Add References
- Right-click on References and select Add Reference…
- Select both of the projects listed from our solution.

Create Our First Unit Tests
Our GetVersion Test
The first thing we are going to do is create our Unit Tests for both GetVersion and and the HelloWorld response to RunScript.
- Right-click on the Unit Test project and select Add->Unit Test…
- If you deleted the previous Unit Test you should see a new UnitTest1.cs file created and opened for editing.
- Right-click on the UnitTest1.cs file and select Rename.
- Set the name to GetVersion and press Enter.
- Select Yes when prompted.

- Now let’s create our Unit Test.
- In general, Unit Tests have three steps:
- Arrange (Setup the scenario to test)
- Act (Run the method to be tested)
- Assert (Verify the method returns as expected)
- Rename TestMethod1() to Execute_GetVersion_ReturnsString().
- Add the following steps to the test.
[TestMethod]
public void Execute_GetVersion_ReturnsString()
{
// Arrange
string expected = "v.0.0.1";
var command = new GetVersion();
// Act
var actual = command.Execute();
// Assert
Assert.AreEqual(expected.GetType(), actual.GetType());
}
- You should see a compile error because we have not created the GetVersion class yet. This is intentional as best practice is to write your test first then write your code.
- Ok. Let’s create our HelloWorld Unit Test.
Note: Eventually we will be setting up our ScriptLinkController to call different commands based on the parameter provided. This is why we are creating a test specifically for HelloWorld rather than generally for RunScript. - Right-click on the Unit Test project and select Add->Unit Test…
- Right-click on UnitTest1.cs and select Rename.
- Set the name to HelloWorld and press Enter.
- Select Yes when prompted.
- Rename TestMethod1() to Execute_HelloWorld_ReturnsOptionObject2015().
- Add the following steps to the test:
Our HelloWorld Tests
Ok. Let’s create our HelloWorld Unit Test. Note: Eventually we will be setting up our ScriptLinkController to call different commands based on the parameter provided. This is why we are creating a test specifically for HelloWorld rather than generally for RunScript.
- Right-click on the Unit Test project and select Add->Unit Test…
- Right-click on UnitTest1.cs and select Rename.
- Set the name to HelloWorld and press Enter.
- Select Yes when prompted.
- Rename TestMethod1() to Execute_HelloWorld_ReturnsOptionObject2015().
- Add the following steps to the test:
[TestMethod]
public void Execute_HelloWorld_ReturnsOptionObject2015()
{
// Arrange
OptionObject2015 expected = new OptionObject2015();
OptionObject2015 optionObject2015 = new OptionObject2015();
string parameter = "";
var command = new HelloWorld(optionObject2015, parameter);
// Act
var actual = command.Execute();
// Assert
Assert.AreEqual(expected.GetType(), actual.GetType());
}
- Let’s add another test to verify that the ErrorCode is returned as expected.
[TestMethod]
public void Execute_HelloWorld_ErrorCodeEquals3()
{
// Arrange
double expected = 3;
OptionObject2015 optionObject2015 = new OptionObject2015();
string parameter = "";
var command = new HelloWorld(optionObject2015, parameter);
// Act
var actual = command.Execute();
// Assert
Assert.AreEqual(expected, actual.ErrorCode);
}
We should now have three unit tests defined and listed in our Test Explorer. These test essentially fail because the code cannot currently compile. So let’s write our code.
Create Our Commands
To make our web service be available for unit testing we are going to pass on the functionality of the API to separate public classes.
- Right-click on the Web Application project and select Add->New Folder.
- Name the folder Commands and press Enter.
- Now we can create our classes.
Create the GetVersion Command
Now we will add our GetVersion Command.
- Right-click on the Commands folder and select Add->Class…
- Set the name to GetVersion.cs and select Add.
- Add a public method called Execute() and set it to return our version string. I incremented it to v.0.0.2 so we can see the difference in the end.
public class GetVersion
{
public string Execute()
{
return "v.0.0.2";
}
}
Create the HelloWorld Command
Next, we will add our HelloWorld command.
- Right-click on the Commands folder and select Add->Class…
- Set the name to HelloWorld.cs and select Add.
- Add a protected OptionObject2015 property named OptionObject2015.
- Be sure to add the using for our class library. In my case it is RS.ScriptLinkDemo.Objects.
- Add a protected string property named Parameter.
- Add a constructor that will take the received OptionObject2015 and parameter and assign them to our protected properties.
- Add a public method named Execute() and set it to return an OptionObject2015 with the ErrorCode 3 and the ErrorMesg “Hello, World!”
- Here is our full class.
public class HelloWorld
{
private OptionObject2015 _optionObject2015;
private string _parameter;
public HelloWorld(OptionObject2015 optionObject2015, string parameter)
{
_optionObject2015 = optionObject2015;
_parameter = parameter;
}
public OptionObject2015 Execute()
{
OptionObject2015 returnOptionObject = new OptionObject2015()
{
EntityID = _optionObject2015.EntityID,
EpisodeNumber = _optionObject2015.EpisodeNumber,
ErrorCode = 3,
ErrorMesg = "Hello, World!",
Facility = _optionObject2015.Facility,
NamespaceName = _optionObject2015.NamespaceName,
OptionId = _optionObject2015.OptionId,
OptionStaffId = _optionObject2015.OptionStaffId,
OptionUserId = _optionObject2015.OptionUserId,
ParentNamespace = _optionObject2015.ParentNamespace,
ServerName = _optionObject2015.ServerName,
SystemCode = _optionObject2015.SystemCode,
SessionToken = _optionObject2015.SessionToken
};
return returnOptionObject;
}
}
Update Our Test Usings
We should now have our two commands create and still have the three compile errors from our Unit Tests. We need to go back to our Unit Tests and add our using for the the Commands namespace.

- Open our GetVersionTests.cs file from the Unit Test project.
- Add a using for our Commands namespace. Mine is RS.ScriptLinkDemo.Soap.Commands;
- Do the same for our HelloWorldTests.cs file.
- Our compile errors should now be gone.

Run Our Tests
Now that our code should be able to compile lets run our tests and see if they pass.
- In our Test Explorer panel, select the Run All Tests icon.
- This should compile the code and run the tests. If all is well all three tests should have passed.

Update Our Web Service
Now that our tests are passing, let update our Web Service to use our new commands instead.
- Open our ScriptLinkController in the web application project.
- Add a using for our Commands namespace. Mine is RS.ScriptLinkDemo.Soap.Commands.
- Update each web method to instantiate the desired command and execute it.
[WebMethod]
public string GetVersion()
{
var command = new GetVersion();
return command.Execute();
}
[WebMethod]
public OptionObject2015 RunScript(OptionObject2015 optionObject2015, string parameter)
{
var command = new HelloWorld(optionObject2015, parameter);
return command.Execute();
}
- At this point I like to run my unit tests again to verify the solution compiles and everything is still Ok.
Verify Web Service is Working
Since we modified our Web Service and that is not tested by the Unit Tests, let’s use SoapUI or Postman to verify the API still responds as expected. I am going to use SoapUI in the steps below.
- Run the debugger for our web application.
- Open SoapUI.
- Expand your ScriptLink demo project so you can see each request.
- Open and run the GetVersion request.
- If everything is correct you get the version number as entered in the command.

- Open and run the RunScript Default request.
- If everything is correct, you should get the expected ErrorCode 3 and ErrorMesg “Hello, World!”

We’re All Set
We have accomplished a lot with these changes.
- We have abstracted our code to minimize the number of edits required to our web service. We will still have to edit it if we want it to do more than HelloWorld. Any edits to HelloWorld should not require any changes to the web service (asmx).
- We have created unit tests to monitor for regressions in our codes. For example, GetVersion should always return a string though we don’t care what the current version number is.
- We have used our external API testing applications (SoapUI or Postman) to verify our API still works as expected.
We are now in great shape to iterate on our code to add functionality and verify everything still works. Next week, we will setup our controller to be able to change behavior based on the parameter supplied.
You must be logged in to post a comment.