コード例 #1
0
            public void Should_Return_Forbidden_WithCustomError(string testId, Func <dynamic, ISpecificationOut <TestClass> > appendErrorCommands, ErrorContentApiHelper.ExpectedErrorContent expected)
            {
                testId.Should().NotBeNull();

                var context = new ScopeBuilderContext();

                var builder = new ScopeBuilder();

                Specification <TestClass> specification = m => appendErrorCommands(m.Forbidden());

                var scope = builder.Build(specification, context);

                if (expected.ShouldBeEmpty(0))
                {
                    scope.ForbiddenErrorId.Should().Be(context.ForbiddenErrorId);
                }
                else
                {
                    scope.ForbiddenErrorId.Should().NotBe(context.ForbiddenErrorId);

                    context.Errors.Keys.Should().Contain(scope.ForbiddenErrorId);

                    expected.Match(context.Errors[scope.ForbiddenErrorId]);
                }
            }
コード例 #2
0
            public void Should_Return_Required_WithCustomError_And_Rules(string testId, Func <dynamic, ISpecificationOut <TestClass> > appendErrorCommands, ErrorContentApiHelper.ExpectedErrorContent expected)
            {
                testId.Should().NotBeNull();

                var context = new ScopeBuilderContext();

                var builder = new ScopeBuilder();

                Specification <TestClass> specification = m => AppendRule(appendErrorCommands(m.Required()));

                var scope = builder.Build(specification, context);

                if (expected.ShouldBeEmpty(0))
                {
                    scope.RequiredErrorId.Should().Be(context.RequiredErrorId);
                }
                else
                {
                    scope.RequiredErrorId.Should().NotBe(context.RequiredErrorId);
                    context.Errors.Keys.Should().Contain(scope.RequiredErrorId);

                    expected.Match(context.Errors[scope.RequiredErrorId]);
                }

                dynamic AppendRule(dynamic api)
                {
                    if (api is IRuleIn <TestClass> ruleIn)
                    {
                        return(RuleExtension.Rule(ruleIn, m => false));
                    }

                    throw new InvalidOperationException("Dynamic api tests failed");
                }
            }
コード例 #3
0
            public void Should_Build_AsNullableScope_When_AsNullable_WithCustomError(string testId, Func <dynamic, ISpecificationOut <int?> > appendContentCommands, ErrorContentApiHelper.ExpectedErrorContent expectedErrorContent, Func <dynamic, ISpecificationOut <int?> > appendSetupCommands, ErrorSetupApiHelper.ExpectedErrorSetup <int?> expectedErrorSetup)
            {
                _ = testId;

                var context = new ScopeBuilderContext();

                var builder = new ScopeBuilder();

                Specification <int> innerSpecification = m => m;

                Specification <int?> specification = m => appendContentCommands(appendSetupCommands(m.AsNullable(innerSpecification)));

                var scope = builder.Build(specification, context);

                scope.CommandScopes.Should().NotBeEmpty();
                scope.CommandScopes.Count.Should().Be(1);
                scope.CommandScopes[0].Should().BeOfType <NullableCommandScope <int> >();

                var nullableCommandScope = (NullableCommandScope <int>)scope.CommandScopes[0];

                nullableCommandScope.Path.Should().Be(expectedErrorSetup.Path);
                nullableCommandScope.ExecutionCondition.Should().BeSameAs(expectedErrorSetup.ShouldExecute);
                nullableCommandScope.ErrorMode.Should().Be(expectedErrorContent.Mode);

                nullableCommandScope.ErrorId.Should().HaveValue();
                context.Errors.Keys.Should().Contain(nullableCommandScope.ErrorId.Value);

                if (expectedErrorContent.ShouldBeEmpty(0))
                {
                    nullableCommandScope.ErrorId.Should().Be(context.DefaultErrorId);
                }
                else
                {
                    nullableCommandScope.ErrorId.Should().NotBe(context.DefaultErrorId);
                    expectedErrorContent.Match(context.Errors[nullableCommandScope.ErrorId.Value], 0);
                }

                context.Scopes.Keys.Should().Contain(nullableCommandScope.ScopeId);

                context.Scopes[nullableCommandScope.ScopeId].Should().BeOfType <SpecificationScope <int> >();
            }
コード例 #4
0
            public void Should_Build_AsCollectionScope_When_AsCollection_WithCustomError(string testId, Func <dynamic, ISpecificationOut <TestParent> > appendContentCommands, ErrorContentApiHelper.ExpectedErrorContent expectedErrorContent, Func <dynamic, ISpecificationOut <TestParent> > appendSetupCommands, ErrorSetupApiHelper.ExpectedErrorSetup <TestParent> expectedErrorSetup)
            {
                testId.Should().NotBeNull();

                var context = new ScopeBuilderContext();

                var builder = new ScopeBuilder();

                Specification <TestClass> innerSpecification = m => m;

                Specification <TestParent> specification = m => appendContentCommands(appendSetupCommands(m.Member(m1 => m1.TestMember, innerSpecification)));

                var scope = builder.Build(specification, context);

                scope.CommandScopes.Should().NotBeEmpty();
                scope.CommandScopes.Count.Should().Be(1);
                scope.CommandScopes[0].Should().BeOfType <MemberCommandScope <TestParent, TestClass> >();

                var memberCommandScope = (MemberCommandScope <TestParent, TestClass>)scope.CommandScopes[0];

                memberCommandScope.Path.Should().Be(expectedErrorSetup.Path ?? "TestMember");
                memberCommandScope.ExecutionCondition.Should().BeSameAs(expectedErrorSetup.ShouldExecute);
                memberCommandScope.ErrorMode.Should().Be(expectedErrorContent.Mode);

                var testParent = new TestParent()
                {
                    TestMember = new TestClass()
                };

                memberCommandScope.GetMemberValue(testParent).Should().BeSameAs(testParent.TestMember);

                memberCommandScope.ErrorId.Should().HaveValue();
                context.Errors.Keys.Should().Contain(memberCommandScope.ErrorId.Value);

                if (expectedErrorContent.ShouldBeEmpty(0))
                {
                    memberCommandScope.ErrorId.Should().Be(context.DefaultErrorId);
                }
                else
                {
                    memberCommandScope.ErrorId.Should().NotBe(context.DefaultErrorId);
                    expectedErrorContent.Match(context.Errors[memberCommandScope.ErrorId.Value], 0);
                }

                context.Scopes.Keys.Should().Contain(memberCommandScope.ScopeId);

                context.Scopes[memberCommandScope.ScopeId].Should().BeOfType <SpecificationScope <TestClass> >();
            }
コード例 #5
0
            public void Should_Build_RuleCommandScope_When_RuleTemplate_WithoutArgs_And_CustomError(string testId, Func <dynamic, ISpecificationOut <TestClass> > appendContentCommands, ErrorContentApiHelper.ExpectedErrorContent expectedErrorContent, Func <dynamic, ISpecificationOut <TestClass> > appendSetupCommands, ErrorSetupApiHelper.ExpectedErrorSetup <TestClass> expectedErrorSetup)
            {
                testId.Should().NotBeNull();

                var context = new ScopeBuilderContext();

                var builder = new ScopeBuilder();

                Predicate <TestClass> predicate = x => true;

                Specification <TestClass> specification = m => appendSetupCommands(appendContentCommands(m.RuleTemplate(predicate, "ruleKey")));

                var scope = builder.Build(specification, context);

                scope.CommandScopes.Should().NotBeEmpty();
                scope.CommandScopes.Count.Should().Be(1);
                scope.CommandScopes[0].Should().BeOfType <RuleCommandScope <TestClass> >();

                var ruleCommandScope = (RuleCommandScope <TestClass>)scope.CommandScopes[0];

                ruleCommandScope.Path.Should().Be(expectedErrorSetup.Path);
                ruleCommandScope.ExecutionCondition.Should().Be(expectedErrorSetup.ShouldExecute);
                ruleCommandScope.ErrorMode.Should().Be(expectedErrorContent.Mode);
                ruleCommandScope.IsValid.Should().BeSameAs(predicate);

                ruleCommandScope.ErrorId.Should().BeGreaterOrEqualTo(0);

                if (expectedErrorContent.ShouldBeEmpty())
                {
                    ruleCommandScope.ErrorId.Should().Be(context.DefaultErrorId);
                }
                else
                {
                    ruleCommandScope.ErrorId.Should().NotBe(context.DefaultErrorId);

                    context.Errors.Keys.Should().Contain(ruleCommandScope.ErrorId);
                    var error = context.Errors[ruleCommandScope.ErrorId];

                    expectedErrorContent.Match(error);

                    error.Args.Should().BeEmpty();
                }
            }