public void WhenRuleFor_RaisesAnException_TheResultIs_Unsuccessful() { var modelMock = new Mock <FakeModel>(); var serviceProviderMock = new Mock <IServiceProvider>(); var ruleForMock = new Mock <IRuleFor <FakeModel> >(); ruleForMock.Setup(s => s.CheckAsync(It.IsAny <FakeModel>())).Throws(new Exception()); serviceProviderMock .Setup(p => p.GetService(typeof(IEnumerable <IRuleFor <FakeModel> >))) .Returns(new List <IRuleFor <FakeModel> >(new[] { ruleForMock.Object })); var ruleset = new RuleSet <FakeModel>(serviceProviderMock.Object); var result = ruleset.Check(modelMock.Object); serviceProviderMock.Verify( v => v.GetService(typeof(IEnumerable <IRuleFor <FakeModel> >)), Times.Exactly(1)); ruleForMock.Verify(v => v.CheckAsync(It.IsAny <FakeModel>()), Times.Exactly(1)); Assert.NotNull(result); Assert.False(result.IsSuccess); }
public void WhenNoRulesFails_TheResultIs_Successful() { var modelMock = new Mock <FakeModel>(); var serviceProviderMock = new Mock <IServiceProvider>(); var ruleFor1Mock = new Mock <IRuleFor <FakeModel> >(); var ruleFor2Mock = new Mock <IRuleFor <FakeModel> >(); ruleFor1Mock.Setup(s => s.CheckAsync(It.IsAny <FakeModel>())) .ReturnsAsync(RuleCheckResult.Success); ruleFor2Mock.Setup(s => s.CheckAsync(It.IsAny <FakeModel>())) .ReturnsAsync(RuleCheckResult.Success); serviceProviderMock .Setup(p => p.GetService(typeof(IEnumerable <IRuleFor <FakeModel> >))) .Returns(new List <IRuleFor <FakeModel> >(new[] { ruleFor1Mock.Object, ruleFor2Mock.Object })); var ruleset = new RuleSet <FakeModel>(serviceProviderMock.Object); var result = ruleset.Check(modelMock.Object); serviceProviderMock.Verify( v => v.GetService(typeof(IEnumerable <IRuleFor <FakeModel> >)), Times.Exactly(1)); ruleFor1Mock.Verify(v => v.CheckAsync(It.IsAny <FakeModel>()), Times.Exactly(1)); ruleFor2Mock.Verify(v => v.CheckAsync(It.IsAny <FakeModel>()), Times.Exactly(1)); Assert.NotNull(result); Assert.True(result.IsSuccess); }
public void FailedRules_AreListedAs_Nonconformities() { var modelMock = new Mock <FakeModel>(); var serviceProviderMock = new Mock <IServiceProvider>(); var ruleFor1Mock = new Mock <IRuleFor <FakeModel> >(); var ruleFor2Mock = new Mock <IRuleFor <FakeModel> >(); var ruleFor3Mock = new Mock <IRuleFor <FakeModel> >(); ruleFor1Mock.Setup(s => s.Code).Returns("Code1"); ruleFor1Mock.Setup(s => s.Description).Returns("Code1 Rule"); ruleFor1Mock.Setup(s => s.CheckAsync(It.IsAny <FakeModel>())) .ReturnsAsync(RuleCheckResult.Fail); ruleFor2Mock.Setup(s => s.Code).Returns("Code2"); ruleFor2Mock.Setup(s => s.Description).Returns("Code2 Rule"); ruleFor2Mock.Setup(s => s.CheckAsync(It.IsAny <FakeModel>())) .ReturnsAsync(RuleCheckResult.Fail); ruleFor3Mock.Setup(s => s.Code).Returns("Code3"); ruleFor3Mock.Setup(s => s.Description).Returns("Code3 Rule"); ruleFor3Mock.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[] { ruleFor1Mock.Object, ruleFor2Mock.Object, ruleFor3Mock.Object, })); var ruleset = new RuleSet <FakeModel>(serviceProviderMock.Object); var result = ruleset.Check(modelMock.Object); serviceProviderMock.Verify( v => v.GetService(typeof(IEnumerable <IRuleFor <FakeModel> >)), Times.Exactly(1)); ruleFor1Mock.Verify(v => v.CheckAsync(It.IsAny <FakeModel>()), Times.Exactly(1)); ruleFor2Mock.Verify(v => v.CheckAsync(It.IsAny <FakeModel>()), Times.Exactly(1)); ruleFor3Mock.Verify(v => v.CheckAsync(It.IsAny <FakeModel>()), Times.Exactly(1)); Assert.NotNull(result); Assert.NotNull(result.Unconformities); Assert.False(result.IsSuccess); Assert.Equal(3, result.Unconformities.Count); Assert.Equal("Code1", result.Unconformities.First().Key); Assert.Equal("Code1 Rule", result.Unconformities.First().Value); Assert.Equal("Code2", result.Unconformities.Skip(1).First().Key); Assert.Equal("Code2 Rule", result.Unconformities.Skip(1).First().Value); Assert.Equal("Code3", result.Unconformities.Skip(2).First().Key); Assert.Equal("Code3 Rule", result.Unconformities.Skip(2).First().Value); }