简体   繁体   中英

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<>. 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. 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.

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.

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:

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. 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. 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() .

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. If you get the same instance of the class then when you call getAnalysisData() you will be receiving the same list as a response.

In that case your test is correct because check1 and check2 are both references to the same list.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM