Mocking The ScriptContext Class

Feb 25, 2014 at 4:41 PM
Edited Mar 6, 2014 at 3:05 PM
I have this idea to mock the ScripContext class so that I can do some integration tests on my development machine. However, the ScriptContext class is sealed; has anyone tried this before? Also what are some of the best practices in terms of writing and testing scripts on a dev machine.
Mar 3, 2014 at 9:59 PM

I did something similar to what you are thinking. I created a standalone ESAPI project which I called PluginTester which allows me to develop and debug a binary plugin script. With little modifications to the script I can either call it from Eclipse or from PluginTester without modifying the code.

I just uploaded my project, feel free to take a look and see if it works for you. The main file (PluginTester.cs) has some instructions on how to set up this project with your own script and I also included a sample script.

Hope it helps,

Mar 6, 2014 at 3:04 PM
Thanks Eduardo, I will take a look at it.
Mar 6, 2014 at 4:40 PM
Edited Mar 6, 2014 at 5:03 PM
Hi, I am getting an error:
  System.ApplicationException was unhandled
  Message=Cannot create application.
   at VMS.TPS.Common.Model.API.Application.CreateApplication(String username, String password) in       B:\VA\Core\TPS.NET\Common\VMS.TPS.Common.Model.API\CustomClasses\ApplicationCustom.cs:line 58
   at PluginTester.Program.Main(String[] args) in C:\?\?\?\PluginTester\Projects\PluginTester\PluginTester.cs:line 59
   at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
   at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
   at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
   at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ThreadHelper.ThreadStart()

Any ideas?
Mar 6, 2014 at 9:36 PM
Are you running the plugin tester standalone executable on a computer that has Eclipse installed?
Mar 6, 2014 at 10:20 PM
No my dev machine does not have Eclipse, my original intent was to mock the ScriptContext class which is been passed into the execute method of my test script, is this possible? I know that ScriptContext is a sealed class therefore this may not be possible using most mocking tools. Can I test my scripts using only the VMS.TPS.Common.*.dll's or is testing only possible on a machine running Eclipse?
Mar 6, 2014 at 10:25 PM
Not sure on your mocking question.

You need Eclipse TPS installed locally in order to execute scripts. When the CreateApplication is called the entire Eclipse runtime is loaded.
Mar 6, 2014 at 10:33 PM
Thanks, for clearing that up.
Mar 7, 2014 at 2:19 AM
Before I had my development environment setup with Eclipse I used to do this. Have the Execute method save the data you need to a file and then call another method that does the work. Then write a separate standalone program that reads that data and calls the worker method from your script, in this way you can develop and debug outside of Eclipse. Something like this:

*** Your script

namespace VMS.TPS
public class Script
public Script()

/// This is the method called by Eclipse when script starts
public void Execute(ScriptContext context//, System.Windows.Window window)
   //Save the data you need to disk here. I used the XML but anything would work.
   //For example save Patient, PlanSetup and CurrentUser
   //Then call the worker method.
   Start(context.Patient,  context.PlanSetup, context.CurrentUser);

/// Main worker method
public static void Start(Patient patient, PlanSetup planSetup, User currentUser)
     //Do the work

*** And your (separate) main program


using VMS.TPS;

public class Test
public static void Main()
      //Read the saved data from disk into patient, planSetup and currentUser
     VMS.TPS.Script.Start(patient, planSetup, currentUser);

Hope this helps!

Mar 7, 2014 at 1:52 PM
Thanks, Eduardo it seems I was not seeing the whole picture. Your solution is exactly what I should have done.