Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create method that returns IDisposable in custom mock class

I am trying to educate some people in the use of real unit tests since most of their automated tests are integration ones.

In order to do so I want to refactor a class so that I can test it in isolation without its dependencies.

For that I have the following restrictions:
1. I cannot use any mocking framework so I have to create custom mocks for this.
2. I cannot modify the class that will be mocked to change any private methods to protected or public, or change the methods to be virtual so I cannot inherit from this class and override the methods.

So in the method that I want to test I have the following using statement:

using(myObject.CreateScope())  
{  
     .... do something  
}

So far what I plan on doing is:
1. Extract an interface from the myObject's class.
2. Inject the interface on the SUT using a property.

Of course the "CreateScope" method will be part of the interface which is defined like this:

public IDisposable CreateScope();

So on my custom mock I would like to do something like:

public IDisposable CreateScope()
{
   return new AnyDisposableObject();
}

My question would be:

Considering that the message that testing classes in isolation provides lots of benefits that integrated tests cannot, what would be the least painful and clearest way to implement the "CreateScope" method in the custom mock?

Should I create an empty fake object whose only purpose is to implement IDisposable?
Should I use any object from the .Net framework that implements IDisposable?
Is there a better way than the two options above?

Thanks in advance.

UPDATE: For the sake of clarity

using(myObject.CreateScope())  
{  
  var local = parameter.Where(o => !myObject.Contains(o)).Select(o).ToList();  
  ...  
  myObject.Register(newInstance);
  ...
  return myObject.GetList();
}

I will be testing the logic within the "using" statement. The myObject object will be mocked just to provide values when calling methods that return them or empty methods for void ones. What the CreateScope method does is to put a lock in a protected Dictionary, but I'm not interested to test that functionality with this.

Hope this clarifies the intent.

like image 305
Sergio Romero Avatar asked Aug 23 '11 16:08

Sergio Romero


People also ask

How do you mock a class in MOQ?

How To Mock Something With Moq. As you can see from the code above, mocking an object is simple. Simply use Mock<> , passing the type you want to mock.

How do I mock a class without an interface?

Simply mark any method you need to fake as virtual (and not private). Then you will be able to create a fake that can override the method.

When to implement IDisposable?

If you access unmanaged resources (e.g. files, database connections etc.) in a class, you should implement IDisposable and overwrite the Dispose method to allow you to control when the memory is freed.

How do I mock a class in Nunit?

The three key steps to using mock objects for testing are: Use an interface to describe the object. Implement the interface for production code. Implement the interface in a mock object for testing.


1 Answers

Try the following Mock object, that implements IDisposable:

class MockDisposable : IDisposable
{
    bool _disposed = false;

    protected virtual void Dispose(bool disposing)
    {
        if (!_disposed) // only dispose once!
        {
            if (disposing)
            {
                // Not in destructor, OK to reference other objects
            }
            // perform cleanup for this object
        }
        _disposed = true;
    }

    public void Dispose()
    {
        Dispose(true);

        // tell the GC not to finalize
        GC.SuppressFinalize(this);
    }

    ~MockDisposable()
    {
        Dispose(false);
    }
}
like image 200
Daniel Peñalba Avatar answered Oct 02 '22 22:10

Daniel Peñalba