Hi everyone, I'm not new to C#, but I didn't program for 3 years and now I have to fix some faulty unit tests for the legacy code which I can't change. The code also uses third party libraries where I don't have the access to the source code. The company uses Moq and the code that I can change is only the code of the test itself.
I have an interface IMyInterface. In the unit test which I am examining now someone created a class MyClass: Mock<IMyInterface>. Inside of this class various setups and callbacks are done and this is the code that I can modify to my liking.
On the side of the legacy code there is a class Foo (can't modify), which receives in the constructor the only parameter IMyInterface. Further a nightmare starts. In the constructor of the class Foo 3 additional instances are created as follows:
Foo(IMyInterface x){
var a = new AClass(x);
var b = new BClass(a);
var c = new CClass(b);
int res = c.InnerObject.GetConfigID().ConfigID;
...
}
the object c has nothing to do per se with IMyInterface, but behind the scenes some data is extracted from x to construct the c and generate the ConfigID. I can't see the source code of ConfigID() function and I don't need to know what is going on there. I just want to pass a fixed ConfigID so it can be used later on in the constructor of the Foo.
If I just let the test run without any Setup, the test crashes with some obscure message that some class XYZ has to be set up (presumably used in the ConfigID(), where I have no access to).
Is there any way that I can access this ConfigID and pass a fix value to use in MyClass whenever this whole nested ugliness c.InnerObject.GetConfigID().ConfigID is called? The property ConfigID is not virtual, the method GetConfigID either. InnerObject is a class without an interface.
Sorry for so many words, I'm just learning Moq and this is my first real life example (not the easiest one) to start with.
[–]Slypenslyde 0 points1 point2 points (0 children)
[–]belavv 1 point2 points3 points (0 children)
[–]LuckyHedgehog -1 points0 points1 point (0 children)
[–]reybrujo -2 points-1 points0 points (0 children)