We have a use case where we need to Mock a class (TestClass2 in the sample), that is inheriting from another class which has a virtual property (TestClass's ILogger in the sample). When using RockCreate to generate the mock with expectations, creating an instance of the mock will cause a null reference exception.
The root cause is due to the fact that the virtual property is being set in the base's constructor, but the Mock's Expectations property hasn't been set yet so when the handler attempts to set the virtual property Expectations is still null. The simple solution is to remove the virtual keyword and everything works fine, but in our case we don't own the code we need to mock.
To Reproduce
Sample project will produce the error RocksTesting.zip
Instead of mocking the "problem class" we can Mock all it's dependencies and then create an instance, in our case that will be a fair bit of effort and requires in-depth knowledge of how the "problem class" is using the dependencies (which we'd like to avoid). Any suggestions for a work around would greatly appreciated?
As a side note, must say I'm really enjoying using the Rocks mocking library, awesome job!
Thanks!
We have a use case where we need to Mock a class (TestClass2 in the sample), that is inheriting from another class which has a virtual property (TestClass's ILogger in the sample). When using
RockCreateto generate the mock with expectations, creating an instance of the mock will cause a null reference exception.The root cause is due to the fact that the virtual property is being set in the base's constructor, but the Mock's Expectations property hasn't been set yet so when the handler attempts to set the virtual property Expectations is still null. The simple solution is to remove the virtual keyword and everything works fine, but in our case we don't own the code we need to mock.
To Reproduce
Sample project will produce the error RocksTesting.zip
Instead of mocking the "problem class" we can Mock all it's dependencies and then create an instance, in our case that will be a fair bit of effort and requires in-depth knowledge of how the "problem class" is using the dependencies (which we'd like to avoid). Any suggestions for a work around would greatly appreciated?
As a side note, must say I'm really enjoying using the Rocks mocking library, awesome job!
Thanks!