C #: web application module testing classes

I am working on a web server application that processes requests received from mobile phones. I have query adapters that adapt phone queries in the query classes that I use in the rest of the application. What each request adapter does is that it accesses one object in a session and changes one of its properties. Now the question is: I want to write a unit test that will test this query adapter, but I don't have a session while I run the test. Is there a way to create a session or something similar to test the full adapter?

Thank you in advance

+3
source share
1 answer

, , - . ,

public interface ISessionableObject
{
    MyData Data { get; set; }
}

2 ,

public class HttpSessionedObject : ISessionableObject
{
    public MyData Data {
        get { return Session["mydata"]; }
        set { Session["mydata"] = value; }
    }
}

public class DictionaryObject : ISessionableObject
{
    private readonly Dictionary<string, MyData> _dict = 
                                    new Dictionary<string, MyData>();

    public MyData Data {
        get { return dict ["mydata"]; }
        set { dict ["mydata"] = value; }
    }
}

Edit:

, , , - :

public class Adapter
{
    public void DoSomething()
    {
         var data = Session["mydata"];
        ...
    }
}

-

public class Adapter
{
    private readonly ISessionableObject _session;

    public Adapter(ISessionableObject session)
    {
        _session = session;
    }

    public void DoSomething()
    {
         var data = _session.Data;
        ...
    }
}

Injection Dependency, ​​ StructureMap, , , , , , ,

public class AdapterUser
{
    public void UsingPhone()
    {
        var adapter = Adapter(new HttpSessionedObject());
        ...
    }
}

[UnitTest]
public class AdapterUserTest
{

    [Test]
    public void UsingPhone()
    {
        var adapter = Adapter(new DictionaryObject());
        ...
    }
}
+5

Source: https://habr.com/ru/post/1719527/


All Articles