Mock objects for unit-testing

What is the best way to create mock objects for unit testing?

Say I have some Polysurfaces, Meshes, and 3D texts that I want to mock for unit testing. I have those objects in Rhino or Grasshopper. What is the quickest way to re-create those objects in C# code?

I see that some objects have a method ToJSON but not something like FromJSON.

How should I go about re-creating mock objects?

1 Like

When using Rhino.Inside in unit test project you could use RhinoCommon directly in tests without mocking.

Sure. But say I have a Polysurface in Rhino that I need to run some tests on. How do I quickly re-create that specific Polysurface in C# code?

One solution is to prepare the set of points yourself and build the surface using RhinoCommon methods, but then the result might depend on the RhinoCommon version.

I am not sure are we having solution to create test surface and reuse it easily in unit tests, which will be independent of RhinoCommon. Good question…

Yeah. You see the challenge here. Say I have a huge mesh in Rhino that I want to run some tests on. While possible, It would be very time consuming to re-create that specific mesh in C# code using RhinoCommon.

I’m not sure why you’d need to mock objects or create them in code when the objects represent the start point for your tests.

What’s the matter with just using RhinoCommon to load the 3dm with the Rhino object(s) you want?

My unit testing is a series of:
load 3dm file
run my own UI actions which are in a script
check my own UI display values which are also in the script
repeat with different test data until done

At the moment I have a couple of model files and I load them manually then execute a special test method on my plugin to read the file with what to do for the test, but I expect to use RhinoDoc.Open shortly to automate that final step.

If you truly want to mock objects and unit test outside the system of Rhino, then you’d have to ‘fake’ the types of Rhino objects that your plugin interacts with.

Except for the case of things like geometric primitives that your plugin extracts from Rhino and then runs calcs on, I’m not sure how much value that adds but I don’t know your exact use cases.

3 Likes