0

I'm retroactively documenting and writing unit tests for some C# code. I would like to determine what code is actually being used and when.

In Visual Studio 2012, is there a way to record all the methods accessed and in what order while walking through specific scenarios?

RWL01
  • 466
  • 1
  • 5
  • 17

3 Answers3

1

You could run your application with a profiler attached, which will give you all accessed methods, call chains, counts, etc.

Community
  • 1
  • 1
jrummell
  • 42,637
  • 17
  • 112
  • 171
0

The Visual Studio Profiler will give you the time spent in each method, and let you inspect the call heirarchy. I don't know if it will give you the exact order they were called in though.

EDIT: Apparently attaching the profiler to a running unit test is harder in VS2012.

Community
  • 1
  • 1
RB.
  • 36,301
  • 12
  • 91
  • 131
0

Are you wanting to execute a test method that make sure that a particular method on a class was invoked ? If so i dont know of a way to do it in VS alone, but you can use a mock framework to create dependency mocks and check values on them. Here is a snippet of a unit test:

 [TestMethod]
        public void HttpPostPrivacyPolicyFacadeSvcErrorTest()
        {
            var controller = ControllerHelper.GetRouteController();
            controller.Session[SessionVariable.User] = new UserInfo() { UserName = Config.Data.Username };

            var idmSvcMock = new Mock<IUserServiceDAO>();
            var facadeSvcMock = new Mock<IFacadeSvcDAO>();

            //setup the facade mock to throw exception to simulate FacadeServiceException
            facadeSvcMock.Setup(x => x.SetPrivacyAcceptanceStatus(It.IsAny<UserInfo>())).Throws<Exception>();
            var userCollectorMock = new Mock<IUserInfoCollector>();
            userCollectorMock.Setup(x => x.GetUserInfo()).Returns(new UserInfo() { UserName = Config.Data.Username });




            controller.FacadeSvc = facadeSvcMock.Object;
            controller.UserServiceDAO = idmSvcMock.Object;
            controller.UserCollector = userCollectorMock.Object;
            controller.DefaultErrorId = "Route_errors_Unabletoprocess";

            //action
            var res = controller.Privacy(new FormCollection());

            //assert
            //make sure we go to the right controller, action, with the correct params.
            res.AssertActionRedirect().ToController("Errors").ToAction("Index").WithParameter("id", "Route_errors_Unabletoprocess");
            //did we call setprivacy once on the mock?
            facadeSvcMock.Verify(x => x.SetPrivacyAcceptanceStatus(It.IsAny<UserInfo>()), Times.Exactly(1));

In the test above i check that SetPrivacyAcceptance was invoked once and only once on my facadeSvcMock instance. More on moq here: Moq

this block of code is actually checking how many times SetPrivacyAcceptanceStatus was invoked: //did we call setprivacy once on the mock? facadeSvcMock.Verify(x => x.SetPrivacyAcceptanceStatus(It.IsAny()), Times.Exactly(1));

the It.IsAny() is the one parameter to that method, so the line above says basically "For any input parameter of type UserInfo verify that we invoked SetPrivacyAcceptanceStatus exactly once."

cobolstinks
  • 6,801
  • 16
  • 68
  • 97