1

I am trying to write a test class to test my validators but I am getting an error will be described below.

I have an test class to test my entities which are only have properties and class is empty for now.

using Xunit;

namespace ECommercial.Business.Tests.EntityValidationTests.FluentValidation
{
    public class EntityValidationTests:IClassFixture<EntityValidationFixture>
    {
        

        [Fact]
        public void validateShouldSuccess(){
        }
    }
}

As seen above, there is a test class has an empty test to run. The problem is that creating an AddressValidator,ProductValidator and others too in the fixture below causes this error message:

Error Message

[xUnit.net 00:00:00.00] xUnit.net VSTest Adapter v2.4.0 (64-bit .NET Core 3.1.8)
[xUnit.net 00:00:00.57]   Discovering: ECommercial.Business.Tests
[xUnit.net 00:00:00.62]   Discovered:  ECommercial.Business.Tests
[xUnit.net 00:00:00.00] xUnit.net VSTest Adapter v2.4.0 (64-bit .NET Core 3.1.8)
[xUnit.net 00:00:00.58]   Starting:    ECommercial.Business.Tests

***<IWROTEHERE> AT THİS LİNE PROGRAM WAITS A FEW SECONDS</IWROTHERE>***

active test running is canceled. reason: Test crashed main process : Stack overflow.

----- Test Execution Summary -----

Total tests: 0. Passed: 0. Failed: 0. Skipped: 0

As seen error says there is stack overflow.

I made fixture code like that and run tests, problem has gone but if I run test after including comment line in constructor problem occurs again.

using System;
using System.Collections.Generic;
using ECommercial.Entites.concrete;
using ECommercial.Business.ValidationRules.FluentValidation.EntityValidators;

namespace ECommercial.Business.Tests.EntityValidationTests.FluentValidation
{
    public class EntityValidationFixture : IDisposable
    {
        
        public static List<Object[]> Entities = new List<Object[]>();

        public static List<Object[]> Validators = new List<Object[]>();
        public static List<Object[]> EntityValidators= new List<Object[]>();

        public EntityValidationFixture()
        {
            // new AddressValidator();
        }

       
        public void Dispose()
        {

        }
    }
}

An example file of custom validator which causes error

using System.Data;
using ECommercial.Entites.concrete;
using FluentValidation;

namespace ECommercial.Business.ValidationRules.FluentValidation.EntityValidators
{
    public class AddressValidator : AbstractValidator<Address>
    {
        public AddressValidator()
        {
            RuleFor(x=>x.Id)
            .PrimaryKeyIdRule();

            RuleFor(x=>x.UserShopId)
            .PrimaryKeyIdRule();

            RuleFor(x=>(int)x.CityId)
            .PrimaryKeyIdRule();
           
        }
        
    }
    
}

This Rules Causes Error Occurs In Validator. If I delete them problem has gone.

Primary Key Constraint Code

public static IRuleBuilderOptions<T,int> PrimaryKeyIdRule<T>(this IRuleBuilder<T,int> ruleBuilder){
            return PrimaryKeyIdRule(ruleBuilder);
        }
        public static IRuleBuilderOptions<T,long> PrimaryKeyIdRule<T>(this IRuleBuilder<T,long> ruleBuilder){
            return ruleBuilder
                .GreaterThanOrEqualTo(0)
                .NotNull();
        }
NoStuff
  • 63
  • 9
  • So adding call to AddressValidator() constructor causes this as you said, but there is no source code of this class in question. – Evk Oct 22 '20 at 19:29
  • @Evk there is actually – E. Shcherbo Oct 22 '20 at 19:30
  • I edited and added the validator codes end of the question. I have made mistake by posting without codes. @Evk – NoStuff Oct 22 '20 at 19:31
  • 1
    By the way, I think since you found what exactly causes the issue you can leave only two last snippets in the question + error message and remove initial `Fixture` class – E. Shcherbo Oct 22 '20 at 19:32
  • Yes, I will delete the first fixture class codes. thanks for suggession. – NoStuff Oct 22 '20 at 19:36
  • Could you please also show definition of `Address` class? – E. Shcherbo Oct 22 '20 at 19:39
  • @E.Shcherbo Added. – NoStuff Oct 22 '20 at 19:42
  • 1
    Hm, interesting. I wonder if you use some custom validators, I'm not sure if the library exposes `PhoneNumber` and `PrimaryKeyIdRule`. It there are custom validators, I think it would be helpful to provide the definition of them also. By the way, you can also try to comment out lines in the `AddressValidator` constructor to find out which exactly rule causes the issue – E. Shcherbo Oct 22 '20 at 19:45
  • Thanks to you I found actual problem point. Edited again. I would never thought that causes the problem. @E.Shcherbo – NoStuff Oct 22 '20 at 19:53
  • 1
    You call the same method here `return PrimaryKeyIdRule(ruleBuilder)`. – E. Shcherbo Oct 22 '20 at 19:55
  • @E.Shcherbo you can post answer. I will tick it – NoStuff Oct 22 '20 at 20:00

1 Answers1

1
public static IRuleBuilderOptions<T,int> PrimaryKeyIdRule<T>(this IRuleBuilder<T,int> ruleBuilder)
{
   return PrimaryKeyIdRule(ruleBuilder);
}

public static IRuleBuilderOptions<T,long> PrimaryKeyIdRule<T>(this IRuleBuilder<T,long> ruleBuilder)
{
   return ruleBuilder
          .GreaterThanOrEqualTo(0)
          .NotNull();
}

You're calling the same method each time actually. It doesn't call long version.

E. Shcherbo
  • 1,128
  • 8
  • 15