在单元测试中使用 swisstopo.dll

Using swisstopo.dll in unit tests

我正在使用 swisstopo 的 dll 将坐标从 LV03 格式迁移到 LV95 格式。

我从 here 下载了 dll。

我想做的第一件事是编写一个单元测试来确认迁移的正确性。 我引用了这两个 dll 并编写了单元测试:

[TestFixture]
public class Lv03ToLv95ConverterTest
{
    [Test]
    [TestCase(600000, 200000, 2600000.0, 1200000.0)]
    public void Convert_WhenGivenLv03In_ThenExpectedLv95Out(double givenLv03Easting, double givenLv03Northing, double expectedLv95Easting, double expectedLv95Northing)
    {
        var actualLv95 = Lv03ToLv95Converter.Convert(givenLv03Easting, givenLv03Northing);
        actualLv95.Easting.ShouldBeEquivalentTo(expectedLv95Easting);
        actualLv95.Northing.ShouldBeEquivalentTo(expectedLv95Northing);
    }
}

public static class Lv03ToLv95Converter
{
    public static Lv95 Convert(double easting, double northing)
    {
        var reframe = new Reframe();
        var height = 0.0;
        reframe.ComputeReframe(
            ref easting,
            ref northing,
            ref height,
            Reframe.PlanimetricFrame.LV03_Civil,
            Reframe.PlanimetricFrame.LV95,
            Reframe.AltimetricFrame.LN02,
            Reframe.AltimetricFrame.LHN95);

        return new Lv95(easting, northing);
    }
}

public class Lv95
{
    public Lv95(double easting, double northing)
    {
        this.Easting = easting;
        this.Northing = northing;
    }

    public double Easting { get; private set; }

    public double Northing { get; private set; }
}

不幸的是,当我想运行测试时,它说:

System.IO.FileLoadException : Could not load the specified file.

at swisstopo.geodesy.htrans.CHtrans.ReadData(Single[,]& data, String filename)

at swisstopo.geodesy.htrans.CHtrans.ComputeHtrans(Double& height, Double east, Double north, HeightTransformation transformation)

at swisstopo.geodesy.reframe.Reframe.TransformAltimetry(Double& height, Double east, Double north, AltimetricFrame altFrameIn, AltimetricFrame altFrameOut)

at swisstopo.geodesy.reframe.Reframe.ComputeReframe(Double& east, Double& north, Double& height, PlanimetricFrame plaFrameIn, PlanimetricFrame plaFrameOut, AltimetricFrame altFrameIn, AltimetricFrame altFrameOut)

at MyProject.Tests.Lv03ToLv95Converter.Convert(Double easting, Double northing) in C:\Projects\MyProject\sources\MyProject.Tests\Lv03ToLv95ConverterTest.cs:line 33

at MyProject.Tests.Lv03ToLv95ConverterTest.Convert_WhenGivenLv03In_ThenExpectedLv95Out(Double givenLv03Easting, Double givenLv03Northing, Double expectedLv95Easting, Double expectedLv95Northing) in C:\Projects\MyProject\sources\MyProject.Tests\Lv03ToLv95ConverterTest.cs:line 21

有人知道这里有什么问题吗? 我以前从未遇到过引用和使用 dll 的问题...

提前致谢

编辑: 我们认为问题可能在于,Reframe 正在调用 Assembly.ExecutingAssembly 并寻找 swisstopo.data 等在找不到的路径中,因为正在执行的程序集是 nunit 运行ner...

我们已尝试在测试中手动加载程序集,但遗憾的是效果不佳。

[Test]
    [TestCase(600000, 200000, 2600000.0, 1200000.0)]
    public void Convert_WhenGivenLv03In_ThenExpectedLv95Out(double givenLv03Easting, double givenLv03Northing, double expectedLv95Easting, double expectedLv95Northing)
    {
        AppDomain.CurrentDomain.Load("swisstopo.data");
        AppDomain.CurrentDomain.Load("swisstopo.reframelib");
        var actualLv95 = Lv03ToLv95Converter.Convert(givenLv03Easting, givenLv03Northing);
        actualLv95.Easting.ShouldBeEquivalentTo(expectedLv95Easting);
        actualLv95.Northing.ShouldBeEquivalentTo(expectedLv95Northing);
    }

我在 NUnit 中加载 "swisstopo.data" 程序集时遇到了同样的问题。 对我来说,使用 MS Test 而不是 NUnit 解决了这个问题。在MS Test中,执行程序集是测试程序集。

[TestMethod]
    public void Convert_WhenGivenLv03In_ThenExpectedLv95Out()
    {
        double givenLv03Easting = 600100, givenLv03Northing = 200100, expectedLv95Easting = 2600100.08309925, expectedLv95Northing = 1200100.06755549;
        var executingAssembly = Assembly.GetExecutingAssembly().FullName;
        Console.WriteLine(executingAssembly);

        var actualLv95 = Lv03ToLv95Converter.Convert(givenLv03Easting, givenLv03Northing);

        actualLv95.Easting.Should().BeApproximately(expectedLv95Easting, 0.00001);
        actualLv95.Northing.Should().BeApproximately(expectedLv95Northing, 0.00001);
    }