Unit Test C# [TestInitialize]

前端 未结 1 1093
时光取名叫无心
时光取名叫无心 2021-01-03 17:37

I am performing Unit Tests on C# Web API controllers - and each one requires several parameters to initialize. I have the following code in each test at the moment but it\'s

相关标签:
1条回答
  • 2021-01-03 18:05

    You can set the variables that you need as fields of the test class and then initialize them in the TestInitialize method.

    class Tests 
    {
        // these are needed on every test
        APIContext apicon;
        XRepository xRep;
        Controller controller;
        RelevantFactoryModel update;
    
        [TestInitialize]
        public void TestInitialize()
        {
            apicon = new APIContext();
            xRep = new xRepository(apicon);
            controller = new relevantController(cRep);
            controller.Request = new HttpRequestMessage();
            controller.Configuration = new HttpConfiguration();
            update = new relevantFactoryModel();
        }   
    }
    

    This way the fields can be accessed from every test

    0 讨论(0)
提交回复
热议问题