lundi 5 janvier 2015

Rhino-Mocking ExpectationViolationException

I started to use Rhino-Mocks and Unit-Tests a few days ago so I'm new in this.


I created a disposable class like this:



public class SomeClass : IDisposable
{
private bool _disposed;

private SomeOtherClass ObjB { get; set; }

public SomeClass(SomeOtherClass b)
{
ObjB = b;
}

public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}

protected virtual void Dispose(bool disposing)
{
if (_disposed)
return;

if (disposing)
{
// set all properties to null

ObjB = null;
// ...
}

_disposed = true;
}
}


and the unit test which should check if the dispose was done correctly:



[TestFixture]
public class SomeClassTests
{
[Test]
public void ShouldDisposeCorrectly()
{
var classB = MockRepository.GenerateStrictMock<SomeOtherClass>()
SomeClass smth;

using (smth = MockRepository.GenerateStrictMock<SomeClass>(classB))
{ }

smth.AssertWasCalled(p => p.Dispose());
}
}


Now when I start the test it throws the following error: Rhino.Mocks.Exceptions.ExpectationViolationException : SomeClass.Dispose(True); Expected #0, Actual #1.


It makes sense, because Dispose() calls Dispose(boolean) but it's protected so I can't access it in the AssertWasCalled method...


Can you help me to find the missing step? :-)


Aucun commentaire:

Enregistrer un commentaire