Skip to content

thiagofis/xunit

Repository files navigation

Clean unit test (powered by xUnit)

Description

The target of this project is to demonstrate how to create a clean unit test suit project. The test method focus should be the relevant data.

Example

The example below will illustrate the basic principle of Clean method test: It focuses on relevant test types and data without the need to explicitly define each value object properties, mock dependencies or objects initializations. Meaning that you should only apply your effort on what really matters.

Test scenario:

Given: a order sent to risk fraud gateway client
When: receive a red flag
Then: returns a fraud label 

Sequance Diagram

Risk Management Sequence Diagram

With default MSTest method

  • MSTest - Native unit testing frameworks for .Net framework.
  • Moq - The most popular and friendly mocking framework for .NET.
[TestMethod]
public void Analyze_ReceivedARedFlagRisk_ReturnsFraudLabel()
{
    //Assemble
    var expected = Label.Fraud;

    var riskFraudClientServiceMock = new Mock<IRiskFraudClientService>();

    var sut = new RiskFraudAnalyzer(riskFraudClientServiceMock.Object);

    var order = new Order
    {
        Id = 64,
        UserName = "Fulano de Tal",
        BillingAddress = new Address { City = "Porto", Country = "Portugal", State = "Porto" },
        ShippingAddress = new Address { City = "Porto", Country = "Portugal", State = "Porto" },
        Items = new List<OrderItem> 
        { 
          new OrderItem { Amount = 10, Brand = "Nike", Description = "Shoes", Type = "Summer" }            
        }
    };

    var riskResponse = new RiskResponse
    {
        Flag = RiskFlag.Red,
        Score = 1000,
        Description = "Blacklist"
    };

    riskFraudClientServiceMock.Setup(x => x.CheckRisk(It.IsAny<OrderInfoRequest>())).Returns(riskResponse);

    //Act
    var result = sut.Analyze(order);

    //Assert
    MS.Assert.AreEqual(expected, result);
}

With clean test method (powered by xUnit)

[AutoFixtureNSustituteData] //Custom inline data attribute
[Theory]
public void Analyze_ReceivedARedFlagRisk_ReturnsFraudLabel(Order order, RiskResponse riskResponse, 
RiskFraudAnalyzer sut)
{
	//Assemble       
	var expected = Label.Fraud;            

	riskResponse.Flag = RiskFlag.Red;

	sut.RiskFraudClientService.CheckRisk(Arg.Any<OrderInfoRequest>()).Returns(riskResponse);

	//Act
	var result = sut.Analize(order);

	//Assert
	Xunit.Assert.Equal(expected, result);
}

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages