public class MyBaseClass
{
protected void OnSomeEvent(EventContext context)
{
SomeType = _someService.DoSomeLogic(context);
}
public SomeType SomeType { get; private set; }
}
MyBaseClass
works some magic to initialise a public property value when an event is triggerd in the process pipline.
public class MyClass : MyBaseClass
{
public string Foo()
{
if (SomeType.SomeLogic())
throw new InvalidOpertaionException();
return "huzzah";
}
}
The flow in other classes depends on SomeType
being initialised.
How can I unit test MyClass.Foo()
?
[TestMethod]
public void Foo_ReturnsHuzzah()
{
var result = new MyClass().Foo();
Assert.IsHuzzah(result);
}
This type of temporal coupling (the event must be raised before the call to Foo
is valid) is often considered a design flaw so the first thing I would do is revisit the design to make sure what I have is right.
If you don't want to go that route, the easiest solution is to initialize SomeType
with a Null Object implementation of SomeType
like so...
class NullSomeType : SomeType
{
public override bool SomeLogic() { return false; }
}
Then you default SomeType with the Null Object in the constructor.
public MyBaseClass()
{
SomeType = new NullSomeType();
}