Hello everyone,
"the art of unit testing" taught me that a try/catch block in a unit
test is bad practice.
So I am hoping someone can help me to find the right approach.
Let's say I have a class "Container" and a class "Foo" whereas the
Container contains a foo class and multiple container classes can
contain the same foo instance.
Container is defined as following:
class Container
{
private Foo foo;
bool closeFooOnClose;
public Container(Foo foo)
{
this.foo = foo;
}
public void Open()
{
if(foo.IsOpenedExclusively)
{
throw new Exception();
}
foo.OpenExclusively();
closeFooOnClose = true;
}
public void Close()
{
if(closeFooOnClose)
foo.Close();
}
}
Foo:
class Foo
{
public bool IsOpenedExclusively();
public void OpenExclusively(){};
public void Close(){};
}
Now I want to test that Container will not try to close the Foo
instance in case the Open scenario failed.
So what comes to my mind first is:
[TestMethod]
public void Close_FailedToOpenFoo_DoesNotTryToClose()
{
var foo = MockRepository.GenerateStrictMock<Foo>();
foo.Expect(f => f.IsOpenedExclusively).Return(true);
var uut = new Container(foo);
try { uut.Open(); }
catch(Exception { }
uut.Close();
foo.VerifyAllExpectations();
}
But.. as explained earlier I think try/catch blocks are to be avoided.
Maybe this is an exception? Or is there a way offered by rhino mocks
to deal with those scenarios?
Or do you know a way to refactor this to make it testable?
Thank you,
Kevin
--
You received this message because you are subscribed to the Google Groups
"Rhino.Mocks" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/rhinomocks?hl=en.