0
0

Xunitでmemberdataで小数点からIntなどへの変換が発生している場合の挙動調べ

Posted at

起こり

  • 消費税計算などのテストを書いてるときmemberdataにdouble型になるような値がある、ただメソッド側の引数はint型指定になっているためmemberdata→どのような変換が行われているか知りたい。

    public static object[][] SampleData
    {
        get
        {
            return new[]
            {
                (100 + 900) * 1.08 + (99 + 600) * 1.1
            }
        }
    }

    [Theory]
    [MemberData(nameof(SampleData))]
    public void someTest(int expected)
    {
        Assert.Equal(100, expected);
    }

みたいな状態。

ひたすらデバッグで追う

https://github.com/xunit/xunit/blob/9f1684f5f74196d952f0085d7bde21aa20df6229/src/xunit.execution/Sdk/Frameworks/Runners/XunitTheoryTestCaseRunner.cs#L162
どうやらこのへんでMemberdataから引数への変換を行っているようだ。

https://github.com/xunit/xunit/blob/9f1684f5f74196d952f0085d7bde21aa20df6229/src/xunit.execution/Sdk/Reflection/Reflector.cs#L46
ここですね。

今回のケースだとDouble→Intへは

Convert.ChangeType(arg, type, CultureInfo.CurrentCulture);

となっている。なので単純な(int)valみたいなキャストとは違っていた。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0