如何调用 Moq 输入参数 属性 中设置的 Action
How to invoke Action set in property of Moq input parameter
我在 ViewModel 中有以下方法。
public void ViewModelMethod()
{
UserDialogs.Confirm(new ConfirmConfig
{
Message = "Dialog message",
OnConfirm = (result) =>
{
if (result)
{
AnotherService.Method();
}
}
});
}
在我的测试中,我有 UserDialogsMock 和 AnotherServiceMock。我正在尝试像下面这样设置 UserDialogsMock。
UserDialogsMock.Setup(s => s.Confirm(It.IsAny<ConfirmConfig>()))
.Callback((ConfirmConfig confirmConfig) => confirmConfig.OnConfirm(true));
如何验证 AnotherServiceMock.Method 被调用?
如果注入了AnotherServiceMock
,只要验证正常即可:
AnotherServiceMock.Verify(s => s.Method(), Times.Once());
这是一个适合我的SSCCE:
namespace ConsoleApplication
{
using System;
using Moq;
using NUnit.Framework;
public class MoqCallbackTest
{
[Test]
public void TestMethod()
{
Mock<IAnotherService> mockAnotherService = new Mock<IAnotherService>();
Mock<IUserDialogs> mockUserDialogs = new Mock<IUserDialogs>();
mockUserDialogs.Setup(s => s.Confirm(It.IsAny<ConfirmConfig>()))
.Callback((ConfirmConfig confirmConfig) => confirmConfig.OnConfirm(true));
SystemUnderTest systemUnderTest = new SystemUnderTest(mockUserDialogs.Object,
mockAnotherService.Object);
systemUnderTest.ViewModelMethod();
mockAnotherService.Verify(p => p.Method(), Times.Never());
}
public interface IAnotherService
{
void Method();
}
public interface IUserDialogs
{
void Confirm(ConfirmConfig config);
}
public class ConfirmConfig
{
public Action<bool> OnConfirm { get; set; }
}
public class SystemUnderTest
{
readonly IAnotherService anotherService;
readonly IUserDialogs userDialogs;
public SystemUnderTest(IUserDialogs userDialogs, IAnotherService anotherService)
{
this.userDialogs = userDialogs;
this.anotherService = anotherService;
}
public void ViewModelMethod()
{
userDialogs.Confirm(new ConfirmConfig { OnConfirm = result =>
{
if (result)
anotherService.Method();
} });
}
}
}
}
我在 ViewModel 中有以下方法。
public void ViewModelMethod()
{
UserDialogs.Confirm(new ConfirmConfig
{
Message = "Dialog message",
OnConfirm = (result) =>
{
if (result)
{
AnotherService.Method();
}
}
});
}
在我的测试中,我有 UserDialogsMock 和 AnotherServiceMock。我正在尝试像下面这样设置 UserDialogsMock。
UserDialogsMock.Setup(s => s.Confirm(It.IsAny<ConfirmConfig>()))
.Callback((ConfirmConfig confirmConfig) => confirmConfig.OnConfirm(true));
如何验证 AnotherServiceMock.Method 被调用?
如果注入了AnotherServiceMock
,只要验证正常即可:
AnotherServiceMock.Verify(s => s.Method(), Times.Once());
这是一个适合我的SSCCE:
namespace ConsoleApplication
{
using System;
using Moq;
using NUnit.Framework;
public class MoqCallbackTest
{
[Test]
public void TestMethod()
{
Mock<IAnotherService> mockAnotherService = new Mock<IAnotherService>();
Mock<IUserDialogs> mockUserDialogs = new Mock<IUserDialogs>();
mockUserDialogs.Setup(s => s.Confirm(It.IsAny<ConfirmConfig>()))
.Callback((ConfirmConfig confirmConfig) => confirmConfig.OnConfirm(true));
SystemUnderTest systemUnderTest = new SystemUnderTest(mockUserDialogs.Object,
mockAnotherService.Object);
systemUnderTest.ViewModelMethod();
mockAnotherService.Verify(p => p.Method(), Times.Never());
}
public interface IAnotherService
{
void Method();
}
public interface IUserDialogs
{
void Confirm(ConfirmConfig config);
}
public class ConfirmConfig
{
public Action<bool> OnConfirm { get; set; }
}
public class SystemUnderTest
{
readonly IAnotherService anotherService;
readonly IUserDialogs userDialogs;
public SystemUnderTest(IUserDialogs userDialogs, IAnotherService anotherService)
{
this.userDialogs = userDialogs;
this.anotherService = anotherService;
}
public void ViewModelMethod()
{
userDialogs.Confirm(new ConfirmConfig { OnConfirm = result =>
{
if (result)
anotherService.Method();
} });
}
}
}
}