4

I have a conditional statement which should looks as follows:

//...
if(_view.VerifyData != true)
{
    //...
}
else
{
    _view.PermanentCancellation.Cancel();
}

where PermanentCancellation is of type CancellationTokenSource.

Im wondering how i should set this up in my mock of _view. All attempts thus far have failed :( and i cant find an example on google.

Any pointers would be appreciated.

2 Answers 2

3

I went a step further and made a factory to create a CancellationTokenManager class that implements the interface. This was because my method has to take CancellationToken and I wanted granular control over .IsCancellationRequested():

My CancellationTokenManagerFactory:

public interface ICancellationTokenManagerFactory
{
  ICancellationTokenManager CreateManager(CancellationToken token);
}

public class CancellationTokenManagerFactory : ICancellationTokenManagerFactory
{
  public ICancellationTokenManager CreateManager(CancellationToken token)
  {
    return new CancellationTokenManager(token);
  }
}

and the manager:

public interface ICancellationTokenManager
{
  bool IsCancellationRequested { get; }

  CancellationToken CancellationToken { get; }
}

public class CancellationTokenManager : ICancellationTokenManager
{
  private readonly CancellationToken _token;

  public CancellationTokenManager(CancellationToken token)
  {
    _token = token;
  }

  public bool IsCancellationRequested
  {
    get
    {
      return _token.IsCancellationRequested;
    }
  }

  public CancellationToken CancellationToken => _token;
}

Then in a class utilizing:

public class MyService
{
  private readonly ICancellationTokenManagerFactory _factory = factory;
  public MyService(ICancellationTokenManagerFactory factory)
  {
    _factory = factory;
  }

  public void StartAsync(CancellationToken token)
  {
    manager = _factory.CreateManager(token);

    //check if cancelled
    if (!manager.IsCancellationRequested())
    }
      // do some work
    }
  }
}

Now if I check cancellation is requested more than once i can mock with different responses each time. Additionally, any interfaces like IHostService can still be utilized because CancellationToken is passed in although it doesn't necessarily matter what is in that token.

1
2

Because CancellationTokenSource.Cancel is not virtual you cannot mock it with moq.

You have two options:

Create a wrapper interface:

public interface ICancellationTokenSource
{
    void Cancel();
}

and an implementation which delegates to the wrapped CancellationTokenSource

public class CancellationTokenSourceWrapper : ICancellationTokenSource
{
    private readonly CancellationTokenSource source;

    public CancellationTokenSourceWrapper(CancellationTokenSource source)
    {
        this.source = source;
    }

    public void Cancel() 
    {
        source.Cancel();
    }

}

And use the ICancellationTokenSource as PermanentCancellation then you can create an Mock<ICancellationTokenSource> in your tests:

// arrange

var mockCancellationTokenSource = new Mock<ICancellationTokenSource>();
viewMock.SetupGet(m => m.PermanentCancellation)
        .Returns(mockCancellationTokenSource.Object)

// act

// do something

// assert

mockCancellationTokenSource.Verify(m => m.Cancel());

And use the CancellationTokenSourceWrapper in your production code.

Or use a mocking framework which supports mocking non virtual members like:

5
  • Hi Nemesv, thanks for replying. Im a little confused though. The ICancellationTokenSource only contains void Cancel(); Where is PermanentCancellation/the return type? (Im sorry if this is a stupid question).
    – Hans Rudel
    Sep 4, 2012 at 13:42
  • You've just shown the Cancel method in your sample. If you use more methods from CancellationTokenSource you need to add all of them to ICancellationTokenSource. And you need to change the type of the PermanentCancellation property to ICancellationTokenSource.
    – nemesv
    Sep 4, 2012 at 13:45
  • If im not mistaken, CancellationTokenSourceWrapper should be a class not an interface. I have then just injected that class into the presenter and also declared a property for the cancellationtokensource in ICancellationTokenSource, thus allowing me to change what its assigned to. I then just setup the Cancel() in my mock of ICancellationTokenSource and verify its called. If you would be willing to change the interface to class (assuming i havent made a mistake) i will accept this as the answer. thanks very much for your help :)
    – Hans Rudel
    Sep 4, 2012 at 16:15
  • Yes CancellationTokenSourceWrapper should be a class, sorry for the confusion. I've fixed my answer.
    – nemesv
    Sep 4, 2012 at 16:57
  • Thanks for fixing it. Np, i managed to figure out how to get it working from the rest of ur answer. thanks again for your time, i really appreciate it!
    – Hans Rudel
    Sep 4, 2012 at 17:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.