[英]Why does my variable change automatically when after I called it in Xunit test?
I'm new at using Xunit test and I am testing a method to add an element to a List<>. 我是使用Xunit测试的新手,正在测试一种将元素添加到List <>的方法。 My problem is that at first I'm assigning my empty list to a variable called check1, then I add 2 elements to my list and then I'm assigning my list with 2 elements to a variable called check2.
我的问题是,首先将空列表分配给名为check1的变量,然后将2个元素添加到列表中,然后将具有2个元素的列表分配给名为check2的变量。 Nothing crazy I would say, but I realized when I used debug mode, that my variable check1 is getting incremented when I add elements to my list.
我不会发疯,但是我意识到当我使用调试模式时,将元素添加到列表中时变量check1会递增。
I don't really know what to do. 我真的不知道该怎么办。 My guess here is that Xunits way to run unit tests is special and it may run multiple test at the same time so it changes check1, but I'm not sure, because I don't understand how (or in which order) Xunit launches the test methods.
我的猜测是Xunits运行单元测试的方式很特殊,它可能同时运行多个测试,因此它会更改check1,但我不确定,因为我不了解Xunit的启动方式(或启动顺序)测试方法。
Here is my test method 这是我的测试方法
[Fact]
public void addAnalysisDataTest()
{
List<AnalysisData> check1 = AnalysisDataManager.getInstance().getAnalysisData();
AnalysisDataManager.getInstance().AddAnalysisData("Name3", true, new List<AnalysisElementData> { new AnalysisElementData(0, "Grid", "", ""), new AnalysisElementData(1, "PivotGrid", "", "") });
AnalysisDataManager.getInstance().AddAnalysisData("Name4", true, new List<AnalysisElementData> { new AnalysisElementData(0, "Grid", "", ""), new AnalysisElementData(1, "PivotGrid", "", "") });
List<AnalysisData> check2 = AnalysisDataManager.getInstance().getAnalysisData();
Assert.True(Equals(check2.Count, check1.Count +2));
}
Here is my AnalysisDataManager class method: 这是我的AnalysisDataManager类方法:
private static AnalysisDataManager instance = null;
private List<AnalysisData> analysis = new List<AnalysisData> { };
private List<int> listIdUsed = new List<int> { }; //Used to automatically create new unique ID
public static AnalysisDataManager getInstance(){...} //Getting the instance of the class
private AnalysisDataManager(){}
public List<AnalysisData> getAnalysisData(){return analysis;} //Returning my List<>
public void AddAnalysisData(string analysisName, bool modificationAllowed, List<AnalysisElementData> elements)
{
int idAnalysis = 0;
if (listIdUsed.Count != 0)
{
idAnalysis = listIdUsed.Max() + 1;
}
listIdUsed.Add(idAnalysis);
analysis.Add(new AnalysisData(idAnalysis, analysisName, modificationAllowed, elements));
}
If you want to know what my other test methods are just ask me and I will update this post. 如果您想知道我的其他测试方法是什么,请问我,我将更新此帖子。
It has nothing to do with XUnit. 它与XUnit无关。 You're always returning the same list, not its copy, so the list referenced by
check1
is exactly the same as the one referenced by check2
, and they are both exactly the same as the private analysis
list inside your AnalysisDataManager
instance. 你总是返回相同的列表,而不是它的拷贝,因此通过引用的名单
check1
是完全一样的一个由引用check2
,他们都完全一样,私人analysis
您的内部列表AnalysisDataManager
实例。 So you basically have three references to the same list, thus, when you insert an element into the list, all of the references "see" that element. 因此,您基本上具有对同一列表的三个引用,因此,当您将元素插入列表时,所有引用都会“看到”该元素。
If you want to return a copy of the list (and you probably should to preserve encapsulation), then your getAnalysisData
should return analysis.ToList()
. 如果要返回列表的副本(并且可能应该保留封装),则
getAnalysisData
应该返回analysis.ToList()
。
It looks to me as though your AnalysisDataManager
is a Singleton, so whenever you call getInstance
you will get back the same static instance of the class. 在我看来,您的
AnalysisDataManager
是一个Singleton,因此,每当调用getInstance
您都将获得该类的相同静态实例。 If you get the same instance of the class then when you call getAnalysisData()
you will be receiving the same list as a response. 如果获得该类的相同实例,则在调用
getAnalysisData()
您将收到相同的列表作为响应。
In that case your test is correct because check1
and check2
are both references to the same list. 在这种情况下,您的测试是正确的,因为
check1
和check2
都是对同一列表的引用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.