コード例 #1
0
        public void ByDefaultCategory_FilterOnly_UncategorizedItems()
        {
            var modelMock           = new Mock <FakeModel>();
            var serviceProviderMock = new Mock <IServiceProvider>();
            var ruleForValidMock    = new Mock <IRuleFor <FakeModel> >();
            var ruleForInvalidMock  = new Mock <IRuleFor <FakeModel> >();

            ruleForValidMock.Setup(s => s.Category).Returns((string)null);
            ruleForValidMock.Setup(s => s.CheckAsync(It.IsAny <FakeModel>()))
            .ReturnsAsync(RuleCheckResult.Success);

            ruleForInvalidMock.Setup(s => s.Category).Returns("Not null");
            ruleForInvalidMock.Setup(s => s.CheckAsync(It.IsAny <FakeModel>()))
            .ReturnsAsync(RuleCheckResult.Fail);

            serviceProviderMock
            .Setup(p => p.GetService(typeof(IEnumerable <IRuleFor <FakeModel> >)))
            .Returns(new List <IRuleFor <FakeModel> >(new[]
            {
                ruleForValidMock.Object,
                ruleForInvalidMock.Object
            }));

            var ruleset = new RuleSet <FakeModel>(serviceProviderMock.Object);
            var result  = ruleset.ByDefaultCategory().Check(modelMock.Object);

            serviceProviderMock.Verify(
                v => v.GetService(typeof(IEnumerable <IRuleFor <FakeModel> >)),
                Times.Exactly(1));

            ruleForValidMock.Verify(v => v.CheckAsync(It.IsAny <FakeModel>()), Times.Exactly(1));
            ruleForInvalidMock.Verify(v => v.CheckAsync(It.IsAny <FakeModel>()), Times.Never());

            Assert.NotNull(result);
            Assert.True(result.IsSuccess);
        }