gpt4 book ai didi

c# - 单元测试不会运行 : process is terminated due to StackOverflowException

转载 作者:行者123 更新时间:2023-11-30 20:25:55 28 4
gpt4 key购买 nike

我正在进行单元测试以测试我编写的算法,不幸的是,由于这个错误,这些测试甚至无法运行:

[10-6-2018 00:12:00 Informational] ========== Run test finished: 0 run (0:00:06,5685302) ========== [10-6-2018 00:12:04 Informational] ------ Run test started ------ [10-6-2018 00:12:09 Error] The active test run was aborted. Reason: Process is terminated due to StackOverflowException.

这些都是我的单元测试:

namespace AlgoritmeTest
{
[TestClass]
public class AlgoritmeTests
{
[TestMethod]
public void TestAddingWagonToTrain()
{
Train train = new Train();
Wagon wagon = new Wagon(10);

List<Wagon> expected = new List<Wagon>();
expected.Add(wagon);

train.AddWagon(wagon);
List<Wagon> actual = train.GetWagons();
Assert.AreEqual(expected, actual, "Failed to add wagon to train");

}

[TestMethod]
public void TestAddingAnimalToWagon()
{
Train train = new Train();
Wagon wagon = new Wagon(10);
Animal animal = new Animal("Goudvis", AnimalFood.Meat, AnimalSize.Big);


List<Animal> expected = new List<Animal>();
expected.Add(animal);

wagon.AddAnimal(animal);
List<Animal> actual = wagon.GetAnimals();

Assert.AreEqual(expected, actual, "Failed to add animal to wagon");
}
[TestMethod]
public void TestIfAnimalsAreReturned()
{
Animal animal = new Animal();

Animal goudvis = new Animal("Goudvis", AnimalFood.Meat, AnimalSize.Big);
Animal visstick = new Animal("Visstick", AnimalFood.Meat, AnimalSize.Big);
animal.animalsFromTheCircus.Add(goudvis);
animal.animalsFromTheCircus.Add(new Animal("Potvis", AnimalFood.Plants, AnimalSize.Medium));
animal.animalsFromTheCircus.Add(new Animal("Zalm", AnimalFood.Meat, AnimalSize.Small));
animal.animalsFromTheCircus.Add(visstick);
animal.animalsFromTheCircus.Add(new Animal("Varkenshaasje", AnimalFood.Plants, AnimalSize.Big));

List<Animal> expected = new List<Animal>();
expected.Add(goudvis);
expected.Add(visstick);

List<Animal> actual = animal.GetAllBigMeatEaters();

Assert.AreEqual(expected, actual, "Failed to get all animals which meet criteria");
}
[TestMethod]
public void TestIfAlgorithmStepWorks()
{
Program program = new Program();
Animal animal = new Animal();

Wagon wagon = new Wagon(10);
Wagon wagon2 = new Wagon(10);

Animal goudvis = new Animal("Goudvis", AnimalFood.Meat, AnimalSize.Big);
Animal visstick = new Animal("Visstick", AnimalFood.Meat, AnimalSize.Big);

animal.animalsFromTheCircus.Add(goudvis);
animal.animalsFromTheCircus.Add(visstick);
animal.animalsFromTheCircus.Add(new Animal("Potvis", AnimalFood.Plants, AnimalSize.Medium));
animal.animalsFromTheCircus.Add(new Animal("Zalm", AnimalFood.Meat, AnimalSize.Small));
animal.animalsFromTheCircus.Add(new Animal("Varkenshaasje", AnimalFood.Plants, AnimalSize.Big));

wagon.AddAnimal(goudvis);
wagon2.AddAnimal(visstick);

Train expected = new Train();

expected.AddWagon(wagon);
expected.AddWagon(wagon2);



program.SetUpProgram(animal.animalsFromTheCircus);
Train actual = program.AddAllMeatEaters();

Assert.AreEqual(expected, actual, "Failed to get all animals which meet criteria and place them in train according to alogrithm");
}
}
}

Animal.cs

public class Animal
{
private string animalName;
private AnimalFood animalFood;
private AnimalSize animalSize;

public Animal(string animalName, AnimalFood animalFood, AnimalSize animalSize)
{
this.animalName = animalName;
this.animalFood = animalFood;
this.animalSize = animalSize;
}
public Animal()
{

}

public List<Animal> animalsFromTheCircus = new DataGenerator().GenerateAnimals(100);

public List<Animal> GetallAnimals()
{
return animalsFromTheCircus;
}

public List<Animal> GetAllBigMeatEaters()
{
return animalsFromTheCircus.Where(animal => animal.getAnimalSize() == AnimalSize.Big && animal.getAnimalFood() == AnimalFood.Meat).ToList();
}
public List<Animal> GetAllBigMeatEaters(List<Animal> animalsFromTheCircus)
{
return animalsFromTheCircus.Where(animal => animal.getAnimalSize() == AnimalSize.Big && animal.getAnimalFood() == AnimalFood.Meat).ToList();
}
public List<Animal> GetAllSmallMeatEaters()
{
return animalsFromTheCircus.Where(animal => animal.getAnimalSize() == AnimalSize.Small && animal.getAnimalFood() == AnimalFood.Meat).ToList();
}
public List<Animal> GetAllBigPlantEaters()
{
return animalsFromTheCircus.Where(animal => animal.getAnimalSize() == AnimalSize.Big && animal.getAnimalFood() == AnimalFood.Plants).ToList();
}

public List<Animal> GetAllMediumMeatEaters()
{
return animalsFromTheCircus.Where(animal => animal.getAnimalSize() == AnimalSize.Medium && animal.getAnimalFood() == AnimalFood.Meat).ToList();
}
public List<Animal> GetAllMediumPlantEaters()
{
return animalsFromTheCircus.Where(animal => animal.getAnimalSize() == AnimalSize.Medium && animal.getAnimalFood() == AnimalFood.Plants).ToList();
}

public List<Animal> GetAllSmallPlantEaters()
{
return animalsFromTheCircus.Where(animal => animal.getAnimalSize() == AnimalSize.Small && animal.getAnimalFood() == AnimalFood.Plants).ToList();
}

public string getAnimalName()
{
return this.animalName;
}

public AnimalFood getAnimalFood()
{
return this.animalFood;
}

public AnimalSize getAnimalSize()
{
return this.animalSize;
}
}

Wagon.cs

   public class Wagon
{
private List<Animal> animals;
private int maxSize;

public Wagon(int maxSize)
{
this.animals = new List<Animal>();
this.maxSize = maxSize;
}

public void AddAnimal(Animal animal)
{
this.animals.Add(animal);
}

public List<Animal> GetAnimals()
{
return this.animals;
}
}

Train.cs

    public class Train
{
private List<Wagon> wagons;

public Train()
{
this.wagons = new List<Wagon>();
}

public void AddWagon(Wagon wagon)
{
this.wagons.Add(wagon);
}

public List<Wagon> GetWagons()
{
return this.wagons;
}
}

我的测试有问题吗,为什么它们不运行?

最佳答案

您显示的代码中没有循环引用,我的灵敏的感觉告诉我问题出在这里

public List<Animal> animalsFromTheCircus = new DataGenerator().GenerateAnimals(100);

更新

每次你创造动物时,它都会产生动物,动物又会产生动物,奇点就会发生。整个宇宙都被吸入

更新 2

这里也存在一个严重的关注点分离问题,我的意思是为什么类动物甚至有动物列表,我知道的动物中没有亚动物(在任何常识)..

关于c# - 单元测试不会运行 : process is terminated due to StackOverflowException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50778995/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com