Moq回调与调用参数

时间:2013-10-07 17:45:56

标签: moq

我使用Moq这三行c#代码,我怎么写一行?

  JobQueueRepository.Setup(r => r.UpdateJobQueueStatus(DefaultJobId, JobStatus.Success)).Callback(() => statuses.Add(JobStatus.Success));
  JobQueueRepository.Setup(r => r.UpdateJobQueueStatus(DefaultJobId, JobStatus.Failed)).Callback(() => statuses.Add(JobStatus.Failed));
  JobQueueRepository.Setup(r => r.UpdateJobQueueStatus(DefaultJobId, JobStatus.Running)).Callback(() => statuses.Add(JobStatus.Running));

感谢您的帮助。

2 个答案:

答案 0 :(得分:1)

您需要一段代码

    JobQueueRepository
        .Setup(it => it.UpdateJobQueueStatus(DefaultJobId, It.IsAny<JobStatus>()))
        .Callback<int, JobStatus>((id, status) => statuses.Add(status));

测试其工作原理的测试

[TestClass]
public class TestClass
{
    [TestMethod]
    public void TestMethod()
    {
        var statuses = new List<JobStatus>();
        var JobQueueRepository = new Mock<IJobQueueRepository>();
        int DefaultJobId = 100500;

        JobQueueRepository
            .Setup(it => it.UpdateJobQueueStatus(DefaultJobId, It.IsAny<JobStatus>()))
            .Callback<int, JobStatus>((id, status) => statuses.Add(status));

        JobQueueRepository.Object.UpdateJobQueueStatus(DefaultJobId, JobStatus.Failed);
        JobQueueRepository.Object.UpdateJobQueueStatus(DefaultJobId, JobStatus.Running);
        JobQueueRepository.Object.UpdateJobQueueStatus(DefaultJobId, JobStatus.Success);

        statuses.Should().HaveCount(3);
        statuses.Should().Contain(JobStatus.Failed);
        statuses.Should().Contain(JobStatus.Running);
        statuses.Should().Contain(JobStatus.Success);
    }

    public enum JobStatus
    {
        Success,
        Failed,
        Running
    }

    public interface IJobQueueRepository
    {
        void UpdateJobQueueStatus(int id, JobStatus status);
    }
}

答案 1 :(得分:0)

您可以轻松创建扩展方法,如下所示。

public class Class1
{
    [Test]
    public void CallBackDemo() {

        var statuses = new List<JobStatus>();
        var jobQueueRepositoryStub = new Mock<IJobQueueRepository>();
        const int defaultJobId = 100500;

        jobQueueRepositoryStub.Setup(r => r.UpdateJobQueueStatus(defaultJobId, JobStatus.Success))
            .Callback( new Action[]
                {
                    () => statuses.Add(JobStatus.Success),
                    () => statuses.Add(JobStatus.Failed),
                    () => statuses.Add(JobStatus.Running)
                });

        var sut = new Sut(jobQueueRepositoryStub.Object);

        sut.Do(defaultJobId);

        Assert.True(statuses.Count == 3);
        Assert.True(statuses.Any(x => x == JobStatus.Failed));
        Assert.True(statuses.Any(x => x == JobStatus.Running));
        Assert.True(statuses.Any(x => x == JobStatus.Success));
    }

回调扩展方法:

   public static class Ext
   {
        public static void Callback<TRepo>(this ISetup<TRepo> repo, IEnumerable<Action> actions ) where TRepo : class {
            foreach (var action in actions) {
               action();
            }
        }
   }

Sut(被测系统)和其他课程:

   public enum JobStatus { Success, Failed, Running }

   public interface IJobQueueRepository {
       void UpdateJobQueueStatus(int id, JobStatus status);
   }

   public class Sut {
       private readonly IJobQueueRepository _repository;

       public Sut(IJobQueueRepository repository) {
           _repository = repository;
       }

       public void Do(int jobId) {
           _repository.UpdateJobQueueStatus(jobId, JobStatus.Success);
           _repository.UpdateJobQueueStatus(jobId, JobStatus.Failed);
           _repository.UpdateJobQueueStatus(jobId, JobStatus.Running);
       }
   }