简体   繁体   English

在Xunit测试中调用变量后,为什么变量会自动更改?

[英]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. 在这种情况下,您的测试是正确的,因为check1check2都是对同一列表的引用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用xUnit编写单元测试时,如何在控制器方法内达到int变量 - How to reach int variable that inside my controller method when i write unit test with xUnit 为什么我的 XUnit 测试在应该失败的时候却成功了? - Why is my XUnit Test succeeding when it should fail? 为什么更新第一个变量值时第二个变量值会改变? - Why does my second variable value change when updating the first? 为什么我的 xunit 测试结果中会出现 System.ArgumentException? - Why do I get System.ArgumentException in my xunit test result? 为什么我的模拟对象在 xUnit 测试项目中不能真正工作? - Why my mock object not working truly in xUnit test project? 为什么我的玩家健康在我想要的时候没有改变 - Why does my player health not change when I want it to 如果dnx版本大于451,为什么xUnit测试在Test Explorer中不可见? - Why does xUnit tests are not visible in Test Explorer if dnx version is > to 451? 为什么没有断言的 xUnit Fact 类型测试总是通过? - Why does an xUnit Fact type test without assertions always passes? 为什么互相调用更快时,下载队列会中断? - Why does my download queue break when called faster after each other? 当我实际上未在代码中进行更改时,为什么椭圆的不透明度会自动更改? - Why is the opacity of my ellipse changing automatically when I don't actually change it in the code?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM