How to Mock activities for unit testing

It is common to Mock integrations, classes, libraries, etc. in unit tests to force a specific behavior/scenario and to then assert that your code handles that scenario appropriately. Does anyone have ideas for how to mock an Activity in UiPath?

For example, I have a workflow that has an Add Queue Item activity. Based on if this is successful or not, the code will behave differently (this activity might fail if there is already a Queue Item with the same Reference in a Queue that has Unique References enabled). I want to test what happens in the success scenario and the failure scenario. There are many other scenarios where mocking an activity would be helpful for writing a unit test.

I’ve adopted this Test Framework which I like quite a bit. I’ve posted my question on that component as well but it’s not specific to just that framework so I’m posting here as well.

It may not be possible to mock activities at this time but I’d love to hear ideas and see if anyone else has interested in this.

3 Likes

In general - it is possible, but is pretty hard to do, mostly because there is no real official support for it (and I don’t mean UiPath, but Workflow Foundation. Or at least I don’t know of any “official” way and wherever I looked it didn’t exist and what can be found is pretty lackluster).

Here is a sample of how to mock a very simple activity using XamlInjector:

While it would be doable in VisualStudio, I don’t see writing these kind of things in UiPath Studio (especially since you need to define mock types, which is not possible within UiPath Studio itself).

I’ve been playing a little bit with this idea and trying to emulate an in-memory doubles creation (by scanning the activity to mock and Reflection.Emit creating the double and pass that to injector), but didn’t have much luck with it and I scraped it.
I think most of the community would be interested in something like this, as long as you don’t need to step out of Studio to do it.

As of right now, I don’t see how to do it directly in UiPath :frowning: But it could work, even if in a limited form (probably void activities would be easiest to start with).