You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在XUnit测试中正确实例化含枚举的CalculateDistanceService

Fixing CalculateDistanceService Instantiation in Your XUnit Test

Got it, let's work through getting your CalculateDistanceService up and running for your test. The main issue here is that you need concrete implementations of your ICalculateDistance<DistanceMethod> interface to populate that _operators list—right now you're stuck because you don't have any classes that actually implement the interface.

Step 1: Create Concrete Implementations for Each DistanceMethod

Since your interface is constrained to Enum (specifically DistanceMethod), you'll need a separate class for each enum value that implements the interface. These classes will return their corresponding enum value and implement the distance calculation logic (you can use simplified logic for testing):

// Geodesic Curve implementation
public class GeodesicCurveDistanceCalculator : ICalculateDistance<DistanceMethod>
{
    public DistanceMethod DistanceMethod => DistanceMethod.GeodesicCurve;

    public DistanceResponse CalculateDistance(DistancePoint point1, DistancePoint point2, MeasureUnit measureUnit)
    {
        // For testing, return a mock value; replace with real logic if needed
        return new DistanceResponse { Distance = 200.5, Unit = measureUnit };
    }
}

// Pythagoras implementation
public class PythagorasDistanceCalculator : ICalculateDistance<DistanceMethod>
{
    public DistanceMethod DistanceMethod => DistanceMethod.Pythagoras;

    public DistanceResponse CalculateDistance(DistancePoint point1, DistancePoint point2, MeasureUnit measureUnit)
    {
        // Mock value for testing
        return new DistanceResponse { Distance = 180.2, Unit = measureUnit };
    }
}

Step 2: Populate the Operators List in Your Test

Now you can instantiate these concrete classes and add them to your _operators list, which will let you create a valid CalculateDistanceService instance:

public class DistanceControllerTest
{
    private readonly List<ICalculateDistance<DistanceMethod>> _operators;

    public DistanceControllerTest()
    {
        // Initialize the list with your concrete calculators
        _operators = new List<ICalculateDistance<DistanceMethod>>
        {
            new GeodesicCurveDistanceCalculator(),
            new PythagorasDistanceCalculator()
        };
    }

    [Fact]
    public void DistanceController_CorrectValue()
    {
        var p1 = new DistancePoint { Latitude = 55.39291815, Longitude = -7.389611015997305 };
        var p2 = new DistancePoint { Latitude = 53.3729581, Longitude = -6.3624744 };

        // Now you can successfully instantiate the service
        var calculateDistanceService = new CalculateDistanceService(_operators);

        // Example: Call the service's method and assert the result
        var geodesicResult = calculateDistanceService.CalculateUsingMethod(p1, p2, MeasureUnit.Kilometers, DistanceMethod.GeodesicCurve);
        Assert.Equal(200.5, geodesicResult.Distance);
    }
}

Why This Works

Each concrete calculator explicitly returns a valid DistanceMethod enum value, so your _operators list will no longer have empty enum values. The CalculateDistanceService can now iterate over the list and find the correct calculator based on the enum, just like it would in production.

内容的提问来源于stack exchange,提问作者gkkkab

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.07 19:57:56