最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - DynamicData doesn't populate correctly test data when using grpc objects - Stack Overflow

programmeradmin5浏览0评论

I use Visual Studio 2022 and MS Test. To generate code from proto file I use Grpc.Tools (2.69.0) and Google.Protobuf (3.29.3) nugets.

I don't know how DynamicData feature works under the hood, but it seems it doesn't populate correctly the step object when the code is generated with protobuf compiler.

Case1:

Step, Element1 and Element2 are generated with protobuf compiler. When TestMethod is invoked the step's Elements are null (see image below).

Case2:

I manually create Step and Element1 and Element2 classes without protobuf compiler. In this case the step's elements are not null.

MS Test

namespace TestProject
{
    [TestClass]
    public class UnitTest
    {
        [TestMethod]
        [DynamicData(nameof(GetTestData), typeof(UnitTest), DynamicDataSourceType.Method)]
        public void TestMethod(Step step)
        {
        }

        public static IEnumerable<object[]> GetTestData()
        {
            yield return new object[] { Step_Element1() };
            yield return new object[] { Step_Element2() };
        }

        private static Step Step_Element1()
        {
            var step = new Step();
            step.Elements.Add(new Element { Element1 = new Element1() });
            return step;
        }

        private static Step Step_Element2()
        {
            var step = new Step();
            step.Elements.Add(new Element { Element2 = new Element2() });
            return step;
        }
    }
}

command.proto

syntax = "proto3";

message Step {
    repeated Element elements = 1;
}

message Element1 {
}

message Element2 {
}

message Element {
    oneof value {
        Element1 element1 = 1;
        Element2 element2 = 2;
    }
}
发布评论

评论列表(0)

  1. 暂无评论