mardi 2 juin 2020

How to setup Mock to already present object as defaul behavior?

I can do this:

var alreadyPresentMyClass = GetMyClass();//retrieving MyClass from somewhere

var mock = new Mock<IMyClass>();
mock.Setup(x=> x.Method1()).Callback(alreadyPresentMyClass.Method1);
mock.Setup(x=> x.Method2()).Callback(alreadyPresentMyClass.Method2);
....
mock.Setup(x=> x.Method666()).Throws(new Exception("BAD LUCK")); //this is only method which is throwing error in this test. So the only one I want to setup.
....
mock.Setup(x=> x.Method1000()).Callback(alreadyPresentMyClass.Method1000);

How one can remove unnecessary garbage of setuping thousands of methods, and instead setup it based on another object as default, like this:

var mock = Mock.CreateByAlreadyPresent(GetMyClass());
mock.Setup(x=> x.Method666()).Throws(new Exception("BAD LUCK"));

Any thoughts? I can write reflection method myself, but is there already such method?

Aucun commentaire:

Enregistrer un commentaire