Moq Mocking and tracking Session values

后端 未结 4 1066
日久生厌
日久生厌 2021-01-13 05:18

I\'m having problems returning a Session value set from mocking using Moq. Using the following

public class TestHelpers
{
 public long sessionValue = -1;
 pu         


        
4条回答
  •  谎友^
    谎友^ (楼主)
    2021-01-13 06:01

    It's because you're returning the value in the getter which was passed by value. So everytime you invoke the getter, you get the same value returned.

    Change the Returns() to use a delegate so it is evaluated every time. That way you will get the correct value every time :)

    Much easier on the eye than a SetupGet embedded inside a SetupSet.

    httpContext.SetupSet(x => x.Session["id"] = It.IsAny())
            .Callback((string name, object val) => sessionValue = (long)val);
    httpContext.SetupGet(x => x.Session["id"]).Returns(() => sessionValue);
    

提交回复
热议问题