Unit testing and mocking for cBots and Indicators

Created at 24 Aug 2023, 09:48
How’s your experience with the cTrader Platform?
Your feedback is crucial to cTrader's development. Please take a few seconds to share your opinion and help us improve your trading experience. Thanks!
JO

josef.van.niekerk

Joined 02.02.2023

Unit testing and mocking for cBots and Indicators
24 Aug 2023, 09:48


Is there a way to mock for example, the Bars property in a Robot, in order to write unit tests for a cBot or Indicator? I've been trying to use xUnit and NSubstitute to mock out Bars but I'm not really having any success at the moment.

I'm trying to mock parts of Bars, for example like so: 

var barSeriesMock = Substitute.For<Bars>();
barSeriesMock.Count.Returns(1000);

This doesn't seem to be working too well, so I must be doing something wrong. Is anyone aware if this is even possible?

I get the following errors when I try to access .Bars:

Object reference not set to an instance of an object.
  at cAlgo.API.Internals.Algo.get_MarketData()
  at cAlgo.API.Internals.Algo.get_Bars()

…surely, having mocked Bars, I should just get the mocked instance?


@josef.van.niekerk
Replies

brendan.inglese
05 Mar 2024, 08:15 ( Updated at: 05 Mar 2024, 13:28 )

Same here.

I'm trying to unit test my algo and struggle to mock anything.

Can you please enable/unseal the appropriate classes so we can completely unit test our cAlgos.


@brendan.inglese