I've blogged before about hosting web services created with Web API inside a test fixture in .Net, what follows is the same idea but in this case hosting a node.js implemented web service, this is using the node.js server I created in this previous post.
Jumping straight to the solution this is what I came up with:
As you can I've managed to reduce the amount required to be specified for each test fixture to 3 parameters:
Before showing the implementation for this class, the test runner & fiddler outputs are shown below:
The base class implementation is shown below, as you can probably guess this uses the Process class from the System.Diagnostics namespace:
You might ask why bother?
I run my test using a continuous testing plugin like nCrunch and it's configured to run test simultaneously. This means if I'm not careful about the port I specify for each test they may clash and cause node to crash on start-up - in the example above I'm not worrying about this as there is only one test, but if there were multiple tests in the fixture I would be using some mechanism to generate a unique valid port number.
Jumping straight to the solution this is what I came up with:
As you can I've managed to reduce the amount required to be specified for each test fixture to 3 parameters:
- working directory for node.js server,
- full path to the node executable,
- node server name and any arguments (note the port number).
Before showing the implementation for this class, the test runner & fiddler outputs are shown below:
The base class implementation is shown below, as you can probably guess this uses the Process class from the System.Diagnostics namespace:
Loading ....
As you can see there isn't much going on, the main thing to notice is the use of an event handler for the Process class Exited event. This is done to check if the process starts up and remains running. The node server will return an exit code '8' if it fails to start the server correctly. The most likely reason this will happen is because the port the derived test fixture is using is already in use by another process, and if this happens I want to fail the test.You might ask why bother?
I run my test using a continuous testing plugin like nCrunch and it's configured to run test simultaneously. This means if I'm not careful about the port I specify for each test they may clash and cause node to crash on start-up - in the example above I'm not worrying about this as there is only one test, but if there were multiple tests in the fixture I would be using some mechanism to generate a unique valid port number.
Comments
Post a Comment