Why my few test case get failed after “Running all” controller and pass individually?

孤者浪人 提交于 2020-01-25 07:52:05

问题


When I execute my all controllers mock test cases then some of the test cases get failed but they got pass individually.

I used Dispose() where I was using the same variable thought the unit test but still, it does not fix.

In my project, I made a common setup Example: AbcMock

public class AbcMock
   {
       public static Mock<IAbcMock> abcMock;

       public static void Setup()
       {
           abcMock = new Mock<IAbcMock>();
           abcMock.Setup(x => x.GetAll(It.IsAny<Guid>())).Returns(1);
       }
   }

Then i have made common setup to all services in another file let's say baseController.cs has following setup

 public Mock<IAbcMock> abcMock ;
 abcMock = AbcMock.abcMock;
 AbcMock.Setup();

My actul test controller

public class AbcMockTestController : baseController
    {
       AbcService abcService ;
       public ClaimExpertRolesMockTestController()
        {
           abcService = new AbcService (foo.Object, boo.Object);
        }

       [Fact]
        public void GetAll_Passing_Valid_Data()
        {
            abcMock.Setup(x => x.GetAll(It.IsAny<Guid>())).Returns(2);
            var result = abcService.GetAll(Guid.NewGuid());
            Assert.Equal(ProcessStatusEnum.Success, result.Status);
        }

       [Fact]
        public void GetAll_Passing_Valid_Data()
        {
            abcMock.Setup(x => x.GetAll(It.IsAny<Guid>())).Returns(3);
            var result = abcService.GetAll(Guid.NewGuid());
            Assert.Equal(ProcessStatusEnum.Success, result.Status);
        }
}

This is my setup for my PROD code. After reading much article i got to know that making my mock object static is making mi issue because of that my test case is getting passed indivisually but failling after running all. So, any suggestions how can I resolve my issue without changing my code much?

来源:https://stackoverflow.com/questions/56865435/why-my-few-test-case-get-failed-after-running-all-controller-and-pass-individu

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!