Skip to content

Throwing an Exception in Callback() Leads to VerificationException #220

@JasonBock

Description

@JasonBock

If I have an interface like this:

public record Person(Guid Id, string Name, uint Age);

public interface IPersonService
{
  Person Get(Guid id);
}

And I create this test:

var id = Guid.Empty;

var personServiceMock = Rock.Create<IPersonService>();
personServiceMock.Methods().Get(id).Callback(_ => throw new NotSupportedException());

var serviceUser = new PersonServiceConsumer(personServiceMock.Instance());

Assert.That(() => serviceUser.Find(id), Throws.TypeOf<NotSupportedException>());
personServiceMock.Verify();

What happens is that the test fails because it's getting a VerificationException. I think it's because of the ordering of the code in the mock:

public global::Person Get(global::System.Guid @id)
{
  if (this.handlers.TryGetValue(0, out var @methodHandlers))
  {
    foreach (var @methodHandler in @methodHandlers)
    {
      if (global::System.Runtime.CompilerServices.Unsafe.As<global::Rocks.Argument<global::System.Guid>>(@methodHandler.Expectations[0]).IsValid(@id))
      {
        var @result = @methodHandler.Method is not null ?
          global::System.Runtime.CompilerServices.Unsafe.As<global::System.Func<global::System.Guid, global::Person>>(@methodHandler.Method)(@id) :
          global::System.Runtime.CompilerServices.Unsafe.As<global::Rocks.HandlerInformation<global::.Person>>(@methodHandler).ReturnValue;
        @methodHandler.IncrementCallCount();
        return @result!;
      }
    }
		
    throw new global::Rocks.Exceptions.ExpectationException("No handlers match for global::Person Get(global::System.Guid @id)");
  }
	
  throw new global::Rocks.Exceptions.ExpectationException("No handlers were found for global::Person Get(global::System.Guid @id)");
}

I think the call to IncrementCallCount() should happen before the callback occurs.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions