Skip to main content
promised check on typos
Source Link
yoger
  • 283
  • 1
  • 7

You could simplify entire test class by extracting part responsible for general configuration. Then you can easili split your test into two, first verifying that your IRestClient is properly called during the process and second that assures tested method returns result provided by this client. For tests that require specific configuration, like throwing exceptions and so on, I found it a nice way to extract this configuration to separate, private methods and calling them when necessary instead of configuring all the scenarios in the Setup method.

[TestFixture]
public class ClientTest
{
    private const long SessionId = 123;
    private Mock<IRestClient> _clientMock;
    private List<string> _expectedSources;
    private Session _session;

    [SetUp]
    public void Setup()
    {
        _expectedSources = new List<string>
                {
                    "Scanner1",
                    "Scanner2"
                };
        _clientMock = new Mock<IRestClient>();
        _clientMock.Setup(x => x.Execute<SessionPrototype>(It.IsAny<IRestRequest>()))
                   .Returns(new RestResponse<SessionPrototype>()
        {
            Data = new SessionPrototype
            {
                Id = SessionId,
                InstalledSources = _expectedSources
            },
            StatusCode = HttpStatusCode.OK
        }).Verifiable();

        _session = new Session(SessionId, clientMock_clientMock.Object);
    }

    [Test]
    public void GetAvailableSources_MustCallTheClientUsingProperRequest()
    {
        clientMock_clientMock.Verify(
            m => m.Execute<SessionPrototype>(
                It.Is<IRestRequest>(
                r => r.Resource == $"session/{sessionIdSessionId}" && 
                     r.Method == Method.GET)));
    }

    [Test]
    public void GetAvailableSources_ReturnsSourcesProvidedByClient()
    {
        var sources = _session.GetAvailableSources();

        CollectionAssert.AreEquivalent(_expectedSources, sources);
    }
}

You could simplify entire test class by extracting part responsible for general configuration. Then you can easili split your test into two, first verifying that your IRestClient is properly called during the process and second that assures tested method returns result provided by this client. For tests that require specific configuration, like throwing exceptions and so on, I found it a nice way to extract this configuration to separate, private methods and calling them when necessary instead of configuring all the scenarios in the Setup method.

[TestFixture]
public class ClientTest
{
    private const long SessionId = 123;
    private Mock<IRestClient> _clientMock;
    private List<string> _expectedSources;
    private Session _session;

    [SetUp]
    public void Setup()
    {
        _expectedSources = new List<string>
                {
                    "Scanner1",
                    "Scanner2"
                };
        _clientMock = new Mock<IRestClient>();
        _clientMock.Setup(x => x.Execute<SessionPrototype>(It.IsAny<IRestRequest>()))
                   .Returns(new RestResponse<SessionPrototype>()
        {
            Data = new SessionPrototype
            {
                Id = SessionId,
                InstalledSources = _expectedSources
            },
            StatusCode = HttpStatusCode.OK
        }).Verifiable();

        _session = new Session(SessionId, clientMock.Object);
    }

    [Test]
    public void GetAvailableSources_MustCallTheClientUsingProperRequest()
    {
        clientMock.Verify(
            m => m.Execute<SessionPrototype>(
                It.Is<IRestRequest>(
                r => r.Resource == $"session/{sessionId}" && 
                     r.Method == Method.GET)));
    }

    [Test]
    public void GetAvailableSources_ReturnsSourcesProvidedByClient()
    {
        var sources = _session.GetAvailableSources();

        CollectionAssert.AreEquivalent(_expectedSources, sources);
    }
}

You could simplify entire test class by extracting part responsible for general configuration. Then you can easili split your test into two, first verifying that your IRestClient is properly called during the process and second that assures tested method returns result provided by this client. For tests that require specific configuration, like throwing exceptions and so on, I found it a nice way to extract this configuration to separate, private methods and calling them when necessary instead of configuring all the scenarios in the Setup method.

[TestFixture]
public class ClientTest
{
    private const long SessionId = 123;
    private Mock<IRestClient> _clientMock;
    private List<string> _expectedSources;
    private Session _session;

    [SetUp]
    public void Setup()
    {
        _expectedSources = new List<string>
                {
                    "Scanner1",
                    "Scanner2"
                };
        _clientMock = new Mock<IRestClient>();
        _clientMock.Setup(x => x.Execute<SessionPrototype>(It.IsAny<IRestRequest>()))
                   .Returns(new RestResponse<SessionPrototype>()
        {
            Data = new SessionPrototype
            {
                Id = SessionId,
                InstalledSources = _expectedSources
            },
            StatusCode = HttpStatusCode.OK
        }).Verifiable();

        _session = new Session(SessionId, _clientMock.Object);
    }

    [Test]
    public void GetAvailableSources_MustCallTheClientUsingProperRequest()
    {
        _clientMock.Verify(
            m => m.Execute<SessionPrototype>(
                It.Is<IRestRequest>(
                r => r.Resource == $"session/{SessionId}" && 
                     r.Method == Method.GET)));
    }

    [Test]
    public void GetAvailableSources_ReturnsSourcesProvidedByClient()
    {
        var sources = _session.GetAvailableSources();

        CollectionAssert.AreEquivalent(_expectedSources, sources);
    }
}
Removed some typo
Source Link
yoger
  • 283
  • 1
  • 7

You could simplify entire test class by extracting part responsible for general configuration. Then you can easili split your test into two, first verifying that your IRestClient is properly called during the process and second that assures tested method returns result provided by this client. For tests that require specific configuration, like throwing exceptions and so on, I found it a nice way to extract this configuration to separate, private methods and calling them when necessary instead of configuring all the scenarios in the Setup method. When it comes to

[TestFixture]
public class ClientTest
{
    private const long SessionId = 123;
    private Mock<IRestClient> _clientMock;
    private List<string> _expectedSources;
    private Session _session;

    [SetUp]
    public void Setup()
    {
        _expectedSources = new List<string>
                {
                    "Scanner1",
                    "Scanner2"
                };
        _clientMock = new Mock<IRestClient>();
        _clientMock.Setup(x => x.Execute<SessionPrototype>(It.IsAny<IRestRequest>()))
                   .Returns(new RestResponse<SessionPrototype>()
        {
            Data = new SessionPrototype
            {
                Id = SessionId,
                InstalledSources = _expectedSources
            },
            StatusCode = HttpStatusCode.OK
        }).Verifiable();

        _session = new Session(SessionId, clientMock.Object);
    }

    [Test]
    public void GetAvailableSources_MustCallTheClientUsingProperRequest()
    {
        clientMock.Verify(
            m => m.Execute<SessionPrototype>(
                It.Is<IRestRequest>(
                r => r.Resource == $"session/{sessionId}" && 
                     r.Method == Method.GET)));
    }

    [Test]
    public void GetAvailableSources_ReturnsSourcesProvidedByClient()
    {
        var sources = _session.GetAvailableSources();

        CollectionAssert.AreEquivalent(_expectedSources, sources);
    }
}

You could simplify entire test class by extracting part responsible for general configuration. Then you can easili split your test into two, first verifying that your IRestClient is properly called during the process and second that assures tested method returns result provided by this client. For tests that require specific configuration, like throwing exceptions and so on, I found it a nice way to extract this configuration to separate, private methods and calling them when necessary instead of configuring all the scenarios in the Setup method. When it comes to

[TestFixture]
public class ClientTest
{
    private const long SessionId = 123;
    private Mock<IRestClient> _clientMock;
    private List<string> _expectedSources;
    private Session _session;

    [SetUp]
    public void Setup()
    {
        _expectedSources = new List<string>
                {
                    "Scanner1",
                    "Scanner2"
                };
        _clientMock = new Mock<IRestClient>();
        _clientMock.Setup(x => x.Execute<SessionPrototype>(It.IsAny<IRestRequest>()))
                   .Returns(new RestResponse<SessionPrototype>()
        {
            Data = new SessionPrototype
            {
                Id = SessionId,
                InstalledSources = _expectedSources
            },
            StatusCode = HttpStatusCode.OK
        }).Verifiable();

        _session = new Session(SessionId, clientMock.Object);
    }

    [Test]
    public void GetAvailableSources_MustCallTheClientUsingProperRequest()
    {
        clientMock.Verify(
            m => m.Execute<SessionPrototype>(
                It.Is<IRestRequest>(
                r => r.Resource == $"session/{sessionId}" && 
                     r.Method == Method.GET)));
    }

    [Test]
    public void GetAvailableSources_ReturnsSourcesProvidedByClient()
    {
        var sources = _session.GetAvailableSources();

        CollectionAssert.AreEquivalent(_expectedSources, sources);
    }
}

You could simplify entire test class by extracting part responsible for general configuration. Then you can easili split your test into two, first verifying that your IRestClient is properly called during the process and second that assures tested method returns result provided by this client. For tests that require specific configuration, like throwing exceptions and so on, I found it a nice way to extract this configuration to separate, private methods and calling them when necessary instead of configuring all the scenarios in the Setup method.

[TestFixture]
public class ClientTest
{
    private const long SessionId = 123;
    private Mock<IRestClient> _clientMock;
    private List<string> _expectedSources;
    private Session _session;

    [SetUp]
    public void Setup()
    {
        _expectedSources = new List<string>
                {
                    "Scanner1",
                    "Scanner2"
                };
        _clientMock = new Mock<IRestClient>();
        _clientMock.Setup(x => x.Execute<SessionPrototype>(It.IsAny<IRestRequest>()))
                   .Returns(new RestResponse<SessionPrototype>()
        {
            Data = new SessionPrototype
            {
                Id = SessionId,
                InstalledSources = _expectedSources
            },
            StatusCode = HttpStatusCode.OK
        }).Verifiable();

        _session = new Session(SessionId, clientMock.Object);
    }

    [Test]
    public void GetAvailableSources_MustCallTheClientUsingProperRequest()
    {
        clientMock.Verify(
            m => m.Execute<SessionPrototype>(
                It.Is<IRestRequest>(
                r => r.Resource == $"session/{sessionId}" && 
                     r.Method == Method.GET)));
    }

    [Test]
    public void GetAvailableSources_ReturnsSourcesProvidedByClient()
    {
        var sources = _session.GetAvailableSources();

        CollectionAssert.AreEquivalent(_expectedSources, sources);
    }
}
Source Link
yoger
  • 283
  • 1
  • 7

You could simplify entire test class by extracting part responsible for general configuration. Then you can easili split your test into two, first verifying that your IRestClient is properly called during the process and second that assures tested method returns result provided by this client. For tests that require specific configuration, like throwing exceptions and so on, I found it a nice way to extract this configuration to separate, private methods and calling them when necessary instead of configuring all the scenarios in the Setup method. When it comes to

[TestFixture]
public class ClientTest
{
    private const long SessionId = 123;
    private Mock<IRestClient> _clientMock;
    private List<string> _expectedSources;
    private Session _session;

    [SetUp]
    public void Setup()
    {
        _expectedSources = new List<string>
                {
                    "Scanner1",
                    "Scanner2"
                };
        _clientMock = new Mock<IRestClient>();
        _clientMock.Setup(x => x.Execute<SessionPrototype>(It.IsAny<IRestRequest>()))
                   .Returns(new RestResponse<SessionPrototype>()
        {
            Data = new SessionPrototype
            {
                Id = SessionId,
                InstalledSources = _expectedSources
            },
            StatusCode = HttpStatusCode.OK
        }).Verifiable();

        _session = new Session(SessionId, clientMock.Object);
    }

    [Test]
    public void GetAvailableSources_MustCallTheClientUsingProperRequest()
    {
        clientMock.Verify(
            m => m.Execute<SessionPrototype>(
                It.Is<IRestRequest>(
                r => r.Resource == $"session/{sessionId}" && 
                     r.Method == Method.GET)));
    }

    [Test]
    public void GetAvailableSources_ReturnsSourcesProvidedByClient()
    {
        var sources = _session.GetAvailableSources();

        CollectionAssert.AreEquivalent(_expectedSources, sources);
    }
}