接口上的非虚拟验证无效

时间:2017-03-25 20:41:12

标签: c# unit-testing moq nservicebus

以下代码正在提出异常,MOQ抱怨:

  

"非虚拟"

上的验证无效

但我正在嘲笑一个界面。我必须做好几次这样的测试,但是我现在无法弄清楚这个问题是什么。

[TestFixture]
public class RegisterDeviceCommandHandlerTests
{
    private RegisterDeviceCommandHandler _handler;
    private readonly Mock<IClientRepository> _clientRepositoryMock = new Mock<IClientRepository>();
    private readonly Mock<IMessageHandlerContext>  _busMock = new Mock<IMessageHandlerContext>();
    private readonly Mock<IClientEncryptionProvider> _clientEncryptionProviderMock = new Mock<IClientEncryptionProvider>();


    [Test]
    public async Task GivenAnUnregisteredDeviceWhenTheDeviceIsAddedThenADeviceRegistrationCompletedEventShouldBePublished()
    {
        _clientRepositoryMock.Setup(x => x.RegisterClient(It.IsAny<byte[]>(), It.IsAny<string>(),
                It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>()))
            .Returns(RegistrationClientOperationResult.Registered);

        var clientIdentity = new ClientIdentity
        {
            HostName = "HostName",
            MacAddress = "MacAddress",
            MachineId = "MachineId"
        };

        _clientEncryptionProviderMock.Setup(x => x.DecryptIdentity(It.IsAny<byte[]>())).Returns(clientIdentity);

        _handler = new RegisterDeviceCommandHandler(_clientEncryptionProviderMock.Object, _clientRepositoryMock.Object)
        {
            Bus = _busMock.Object
        };

        await _handler.HandleAsync(new RegisterDeviceCommand
        {
            Identity = new byte[] { 1, 2 }
        });

        _busMock.Verify(x => x.Publish(It.IsAny<DeviceRegistrationCompletedEvent>()));
    }
}

1 个答案:

答案 0 :(得分:2)

找到它。 IMessageHandlerContext继承自IPipelineContext,其 <div *ngIf="showLogout();then userLogout else adminlogout"></div> <ng-template #userLogout><a (click)="onUserLogoutClick()" href="#">Logout</a></li></ng-template> <ng-template #adminlogout><a (click)="onAdminLogoutClick()" href="#">Logout</a></li></ng-template> 方法

Publish

接受一个参数的extension method可用于接口和该方法。

/// <summary>
/// Publish the message to subscribers.
/// </summary>
/// <param name="message">The message to publish.</param>
/// <param name="options">The options for the publish.</param>
Task Publish(object message, PublishOptions options);

因此,为了满足使用模拟的扩展方法,您需要验证

/// <summary>
/// Publish the message to subscribers.
/// </summary>
/// <param name="context">The instance of <see cref="IPipelineContext" /> to use for the action.</param>
/// <param name="message">The message to publish.</param>
public static Task Publish(this IPipelineContext context, object message)
{
    Guard.AgainstNull(nameof(context), context);
    Guard.AgainstNull(nameof(message), message);

    return context.Publish(message, new PublishOptions());
}