1

我需要断言一个基WorkItem被传递一次给一个方法,而一个派生SendEmailWorkItem被传递一次。

repo = new Mock<IWorkItemRepository>();
repo.Setup(x => x.Add(It.IsAny<WorkItem>())).Verifiable();
repo.Setup(x => x.Add(It.Is<SendEmailWorkItem>(wi => wi.ResponsibleId == responsibleGuid))).Verifiable();

Moq 表示第一个期望发生两次,第二个期望发生 0 次。我知道发生这种情况是因为这些类型位于相同的继承层次结构中。(以下断言采用 MSpec 语法)

It should_add_new_claim_workitem = () => repo.Verify(x => x.Add(MockIt.IsAny<WorkItem>()), Times.Once());
It should_add_security_service_notification_workitem_with_same_responsible_as_new_claim = () => repo.Verify(x => x.Add(MockIt.Is<SendEmailWorkItem>(wi => wi.ResponsibleId == responsibleGuid)), Times.Once());

如何根据类型限制期望?

4

3 回答 3

2

以相反的顺序指定设置?

于 2013-02-19T22:50:30.813 回答
1

使用It.Is<>并检查类型:

namespace MoqTests
{
    using Moq;
    using NUnit.Framework;

    public class WorkItem
    {
        public int Id { get; set; }
    }

    public class SendEmailWorkItem : WorkItem
    {
        public string ResponsibleId { get; set; }
    }

    public interface IWorkItemRepository
    {
        void Add(WorkItem workItem);
    }

    public class Worker
    {
        private readonly IWorkItemRepository repository;

        public Worker(IWorkItemRepository repository)
        {
            this.repository = repository;
        }

        public void DoWork()
        {
            var workItem = new WorkItem{Id = 1};
            repository.Add(workItem);

            var emailWorkItem = new SendEmailWorkItem{Id = 2, ResponsibleId = "responsible"};
            repository.Add(emailWorkItem);
        }
    }

    [TestFixture]
    public class MoqTest
    {
        [Test]
        public void Should_add_WorkItem_and_SendEmailWorkItem()
        {
            //arrange
            var repository = new Mock<IWorkItemRepository>(MockBehavior.Strict);
            repository.Setup(r=>r.Add(It.Is<WorkItem>(item => item.GetType() == typeof(WorkItem)))).Verifiable();
            repository.Setup(r=>r.Add(It.Is<SendEmailWorkItem>(item=>item.ResponsibleId == "responsible"))).Verifiable();
            var worker = new Worker(repository.Object);

            //act
            worker.DoWork();

            //assert
            repository.Verify(r => r.Add(It.Is<WorkItem>(item => item.GetType() == typeof(WorkItem))), Times.Once());
            repository.VerifyAll();
        }
    }
} 
于 2013-02-19T22:49:37.500 回答
-1

使用这种设置:

public interface IWorkItemRepository
{
    void Add(WorkItem workItem);
    IQueryable<WorkItem> GetAll();
    void Finish(int workitemId);
}

public class WorkItem
{
    public int Id { get; set; }
    public string Name { get; set; }        
}

public class SendEmailWorkItem : WorkItem
{
    public Guid ResponsibleId { get; set; }
}

这会起作用,但可能不是最优雅的解决方案

[TestFixture]
public class WorkItemRepositoryTests
{
    [Test]
    public void Test()
    {
        // Arrange
        var responsibleGuid = new Guid("11111111-2222-3333-4444-555555555555");
        var workitemRepo = new Mock<IWorkItemRepository>();
        workitemRepo.Setup(x => x.Add(It.IsAny<WorkItem>())).Verifiable();
        workitemRepo.Setup(x => x.Add(It.Is<SendEmailWorkItem>(wi => wi.ResponsibleId == responsibleGuid))).Verifiable();

        // Act   
        workitemRepo.Object.Add(new WorkItem() {Id = 1});
        workitemRepo.Object.Add(new SendEmailWorkItem(){ResponsibleId = responsibleGuid});

        // Assert
        workitemRepo.Verify(x => x.Add(It.Is<WorkItem>(item => item.Id == 1 )), Times.Once());
        workitemRepo.Verify(x => x.Add(It.Is<SendEmailWorkItem>(wi => wi.ResponsibleId == responsibleGuid)), Times.Once());            
    }

}

于 2013-02-19T16:05:30.210 回答