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();
}