简体   繁体   中英

How to test in xunit complex method

How can I test in asp.net core 2.0 following method which exists in separate project than my test project? for example like this:

 public partial class LoanRequestServiceController : BaseServiceController
 {
    public ServiceDTO<AP_CBO> AddCBO(AP_CBO cbo)
    {
        ServiceDTO<AP_CBO> dto = new ServiceDTO<AP_CBO>();

        try
        {
            using (var scope = new TransactionScope(TransactionScopeOption.Required, new TransactionOptions { IsolationLevel = IsolationLevel.Snapshot }))
            {
                cbo.ID_CBO = 333;
                dto.Data = cbo;
                scope.Complete();
            }
        }
        catch (Exception ex)
        {
            dto.Error = new ServiceError(ex);
            Globals.Logger.Error(ex);
        }
        finally
        {
            //Globals.CastleComponentsContainer.Release(LoanRequestDAL);
        }
        return dto;
    }
 }

I tested some "light" methods such as if service method returns SucessCode and it works. Here is my test class:

    [Theory]
    [InlineData("/Sample/AddCBO")]
    public async Task Test_AddCBO(string url)
    {
        //Arrange
        var client = _factory.CreateClient();

        //Act
        var response = await client.GetAsync(url);

        //Assert

        response.EnsureSuccessStatusCode();
        //Compare two dto objects AP_CBO
        //object expected = new AP_CBO { properties... }
        // object responseObject = response.Content...
        //Assert.Equal(expected, responseObject);
    }

I don't know how to test an object with muliple properties. Maybe I need to use Moq? Theoretically, this method would be go to the DAL (DatabaseAccess Layer) and return from database packed object and returns to the api, or in my case back into test.

First off, you have to decide which level of tests you want to write.

If you're writing a Unit test , you should mock any and all external integrations (in your case I can identify HTTP request -> Controller and Controller -> Database ). This is the foundation of your functional testing. So if you're writing unit tests, yes, you should use a mocking framework such as NSubstitute or Moq (and only test your method's behavior by calling it).

The test sample you posted looks to me like an integration test since you're including the integration HTTP request -> Controller . In this case I would seed the database with data (if relevant) and actually call your API endpoint (as you're already doing).

To check the content (DTO) of the response in ASP.Net Core you have to do the following:

// ...
var response = await client.GetAsync(url);

response.EnsureSuccessStatusCode();

var content = await httpResponseMessage.Content.ReadAsStringAsync();
var serviceDto = JsonConvert.DeserializeObject<ServiceDTO<AP_CBO>>(content); // Only for Json
// Validate serviceDto

It is pretty long topic for detailed explanation here ; i think it will be better if you follow a sample and read the details.

I assume that you are going to write unit test; for unit test i can recommend this tutorial that may help you . check this please

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