I'm attempting to isolate and test a particular class in an existing code base which is derived from some base and has a private class member that I want to mock:
public class Derived : Base
{
protected Something<Other> _otherSomething = new Something<Other>();
}
Specifically, I'd like to use the tests to verify that Derived
raises some event DoSomething
in response to _otherSomething
raising the ItHappened
event.
In an attempt to accomplish this, I created in my test project a MockedDerived
class which is a Derived
:
public class MockDerived : Derived
{
public Something<Other> OtherSomething
{
set { _otherSomething = value; }
}
}
I'm using NSubstitute as part of my MSTest test method to mock out a class and I want to do something like so:
[TestMethod]
public void TestMethod1()
{
var sub = Substitute.For<Something<Other>>();
MockedDerived md = new MockedDerived();
bool wasRaised = false;
md.DoSomething += (sender, args) => wasRaised = true;
md.OtherSomething = sub;
sub.itHappened += Raise.Event<ItHappenedEventHandler<Other>>(new ItHappenedEventArgs<Other>());
Assert.IsTrue(wasRaised);
}
Where I'm having some difficulty is in that there is a fair amount of baggage that the Base
class brings to the table, namely it creates some objects of other classes and those have some timers and ultimately involve a database.
It gets messy.
And all I really want to do is ensure that this Derived
class exhibits particular behaviors and responds as expected.
Any suggestions on how to isolate the testing of this Derived
behavior from the messy Base
and its various members? Perhaps I'm just not seeing the obvious solution.