lundi 7 novembre 2016

Unit Test C# Sessions

I am unit testing my API calls, the problem is every time we do an API call, .Net creates another HttpContext.Current.Session instance. So for short, we can't use session variables.

Any thoughts on how this will be handled.

I'm hoping for something that does not alter the existing codes, just the unit test codes.

Unit Test Code

var car = getByUrl("getCarBySessionId");
var train = getByUrl("getTrainBySessionId");

These are the API

[Route("api/getCarBySessionId")]
public Car GetCarBySessionId(int id)
{
var sessionId = HttpContext.Current.Session[SessionId];
_service.GetCarBySessionId(sessionId);
}

[Route("api/getTrainBySessionId")]
public Train GetTrainBySessionId(int id)
{
    var sessionId = HttpContext.Current.Session[SessionId];
    _service.GetTrainBySessionId(sessionId);
}

What happens here is that the 2 sessionIds are not the same. I was expecting it to have the same values but it generates 2 different sessionIds.

Aucun commentaire:

Enregistrer un commentaire