在Task.Run中调用了验证Mock方法

时间:2015-07-30 15:55:17

标签: c# unit-testing moq mstest

如果在传递给Task.Run的委托中调用方法本身时,如何验证是否在模拟上调用了方法?到时间mock.Verify被调用时,任务仍未执行。

我在await Task.Delay之前尝试了mock.Verify,但这似乎让测试跑者不知所措。

使用Task.Run的原因是卸载逻辑,以防止攻击者在执行时能够区分系统中是否存在电子邮件地址。

using System.Threading.Tasks;
using System.Web.Mvc;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;

namespace AsyncTesting
{
    class MyController : Controller
    {
        public IEmailService EmailService { get; set; }

        public MyController(IEmailService emailService)
        {
            EmailService = emailService;
        }

        public ViewResult BeginPasswordReset(string emailAddress)
        {
            BeginPasswordResetAsync(emailAddress);

            return View();
        }

        private Task BeginPasswordResetAsync(string emailAddress)
        {
            return Task.Run(delegate
            {
                EmailService.Send(emailAddress);
            });
        }

    }

    internal interface IEmailService
    {
        void Send(string emailAddress);
    }

    internal class MyControllerTests
    {
        [TestMethod]
        public void BeginPasswordReset_SendsEmail()
        {
            var mockEmailService = new Mock<IEmailService>();
            var controller = new MyController(mockEmailService.Object);
            const string emailAddress = "email@domain.com";

            controller.BeginPasswordReset(emailAddress);

            mockEmailService.Verify(es=>es.Send(emailAddress));
        }
    }
}

2 个答案:

答案 0 :(得分:8)

在你的任务中,你可以设置一个ManualResetEvent(我们的测试代码阻止使用类似的东西:

Assert.IsTrue(yourEvent.WaitForOne(TimeSpan.FromSecond(<max time you want to wait>), "the event failed to run");
像这样:

public void BeginPasswordReset_SendsEmail()
{
    const string emailAddress = "email@domain.com";

    ManualResetEvent sendCalled= new ManualResetEvent(false);

    var mockEmailService = new Mock<IEmailService>();
    mockEmailService.Setup(m => m.Send(emailAddress)).Callback(() =>
    {
        sendCalled.Set();
    });

    var controller = new MyController(mockEmailService.Object);

    controller.BeginPasswordReset(emailAddress);

    Assert.IsTrue(sendCalled.WaitOne(TimeSpan.FromSeconds(3)), "Send was never called");
    mockEmailService.Verify(es => es.Send(emailAddress));
}

答案 1 :(得分:1)

一些快速的研究看起来MSTest可以实现。 e.g。

[TestMethod]
public async Task BeginPasswordResetAsync();
{
    await BeginPasswordResetAsync("emailAddress");

    mockEmailService.Verify...
}