简体   繁体   中英

Unit test for Api controller

I try to write a unit test for a Api Controller. It is a C# mvc application. And the unit test also works with Moq.

This is the controller:

public sealed class CaseController : ApiControllerBase
{    
    [HttpGet]
    [Route("{id:int}")]
    public ICaseDto<object> Get(int id)
    {
        return _caseProcess.GetCase(id);
    }
}

And this is the interface:

public interface ICaseProcess
{
    ICaseDto<object> GetCase(int id);
}

And this is the method:

internal sealed class CaseProcess : ProcessBase, ICaseProcess
{
    public ICaseDto<object> GetCase(int id)
    {
        var caseEntity = GetCaseById(id);

        CaseProcessHelper helper = GetHelper(caseEntity);
        object details = helper.GetReferenceDetails();

        ICaseDto<object> resultDto = CaseDto.Create(details);
        Mapper.Map(caseEntity, resultDto);
        return resultDto;
    }
}

And I try to write the Unit test like this:

[TestMethod]
public void CaseConroller_ReturnDetailData_Test()
{
    //Arrange
    var dto = new Case();
    var mockService = new Mock<ICaseProcess>();
    var Casecontroller = new CaseController(ICaseDto<Case>);
    var expected = new CaseEditDto();
    //var dataSource = new

    CaseProcess.Setup(a => a.Should(dto))

    //Act

    // var result = "hallo";

    //Assert
    result.Should().BeEquivalentTo();
}

So how exactly you have to write the Unit test?

I have a Detail view and then the Api has to return the data of the detail view.

The id is an int, like this: api/support/cases/100000002 .

The constructor looks like this:

 public CaseController(ICaseProcess caseProcess)
        {
            _caseProcess = caseProcess;
        }

I have it now like this:

 //Arrange
            var dto = new Case();
            var mockService = new Mock<ICaseProcess>();
            var casecontroller = new CaseController(CaseProcess.Object);
            var expected = new CaseEditDto();

            //Act

             var result = casecontroller.Get(100000001);

            //Assert
            result.Should().BeEquivalentTo(expected);

But the output is like this:

{
    "details": {
        "name": "Val van Urquhart",
        "dateOfBirth": "11 september 1933"
    },
    "dateSubmittedFormatted": "1 januari 2018",
    "dateClosedFormatted": null,
    "sample": false,
    "comments": [
        {
            "id": 1,
            "account": "todo@local.nl",
            "date": "08-11-2018 17:13",
            "text": "ABC"
        }
    ],
    "id": 100000001,
    "status": 103,
    "substatus": null,
    "verdict": null,
    "owner": 101,
    "dateSubmitted": "01-01-2018",
    "dateClosed": null,
    "type": 100,
    "reference": "123459850"
}

I try it like this:

 [TestMethod]
        public void CaseController_GetCase_Test()
        {
            var CaseObject = new CaseDto<object>()
            {
                Id = 100000001,
                Verdict = null,                
                DateSubmitted = "01-01-2018",
                DateClosed = null,
                Reference = "123459850"

            };        




            var CaseTest = new CaseEditDto<object>();
            // Case entity =   UnitOfWork.GetRepository<Case>()

        }

Based on the shown snippets the assumption is that the controller looks something like

public sealed class CaseController : ApiControllerBase {
    private readonly ICaseProcess caseProcess;

    public CaseController(ICaseProcess caseProcess) {
        this.caseProcess = caseProcess;
    }

    [HttpGet]
    [Route("{id:int}")]
    public ICaseDto<object> Get(int id) {
        return caseProcess.GetCase(id);
    }
}

A very simple test would be to mock the dependency, inject it into the subject under test, exercise the test and then assert the expected behavior

[TestMethod]
public void CaseConroller_ReturnDetailData_Test() {
    //Arrange
    //mock the dependency
    var id = 100000001;
    var expected = Mock.Of<ICaseDto<object>>();
    var mockService = new Mock<ICaseProcess>();
    mockService.Setup(_ => _.GetCase(id)).Returns(expected);
    //inject it into the subject under test
    var subject = new CaseController(mockService.Object);

    //Act
    var actual = subject.Get(id); //exercise the test

    //Assert
    //assert the expected behavior
    actual.Should().BeEquivalentTo(expected);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM