伪造合流.netKafka消费者

jq6vz3qz  于 2021-06-08  发布在  Kafka
关注(0)|答案(2)|浏览(288)

环境:.net 4.5。假货:4.0.0
我试图创建一个kafka consumer的假对象,下面是我使用的语法:

var fakeconsumer = A.Fake<Consumer<Null, string>>((x => x.WithArgumentsForConstructor(() => new Consumer<Null, string>(A.Dummy<IEnumerable<KeyValuePair<string, object>>>(), A.Dummy<IDeserializer<Null>>(), A.Dummy<StringDeserializer>()))));

Kafka客户机的代码如下:https://github.com/confluentinc/confluent-kafka-dotnet/blob/master/src/confluent.kafka/consumer.cs
如您所见,我使用构造函数所需的正确参数调用假调用。但是,我不断收到以下错误消息:“没有构造函数与传递给构造函数的参数匹配。”。
非常感谢您的帮助。
谢谢您
编辑:
在fakeiteasy.creation.castledynamicproxy.castledynamicproxygenerator.createproxygeneratorresult(type typeofproxy,proxygenerationoptions options,ienumerable 1 additionalInterfacesToImplement, IEnumerable c:\projects\fakeiteasy\src\fakeiteasy\creation\castledynamicproxy\castledynamicproxygenerator.cs:fakeiteasy.creation.castledynamicproxygenerator.castledynamicproxygenerator.generateproxy的第125行 1 additionalInterfacesToImplement, IEnumerable c:\projects\fakeiteasy\src\fakeiteasy\creation\castledynamicproxy\castledynamicproxygenerator.cs:fakeiteasy.creation.fakeobjectcreator.generateproxy的第86行中的1个参数forconstructor 1 argumentsForConstructor) in C:\projects\fakeiteasy\src\FakeItEasy\Creation\FakeObjectCreator.cs:line 113 at FakeItEasy.Creation.FakeObjectCreator.CreateFake(Type typeOfFake, IProxyOptions proxyOptions, DummyCreationSession session, IDummyValueResolver resolver, Boolean throwOnFailure) in C:\projects\fakeiteasy\src\FakeItEasy\Creation\FakeObjectCreator.cs:line 36 at FakeItEasy.Creation.DefaultFakeAndDummyManager.CreateFake(Type typeOfFake, Action c:\projects\fakeiteasy\src\fakeiteasy\creation\defaultfakeanddummymymanager.cs中的1 optionsbuilder):c:\projects\fakeiteasy\src\fakeiteasy\a.cs中fakeiteasy.a.fake[t]的第41行(action`1 optionsbuilder)

xoshrz7s

xoshrz7s1#

我想我重现了你的问题。以下是我看到的全部例外:

FakeItEasy.Core.FakeCreationException : 
  Failed to create fake of type Confluent.Kafka.Consumer`2[Confluent.Kafka.Null,System.String] with the specified arguments for the constructor:
    No constructor matches the passed arguments for constructor.
    An exception of type System.ArgumentException was caught during this call. Its message was:
    'group.id' configuration parameter is required and was not specified.
       at Confluent.Kafka.Consumer..ctor(IEnumerable`1 config)
       at Confluent.Kafka.Consumer`2..ctor(IEnumerable`1 config, IDeserializer`1 keyDeserializer, IDeserializer`1 valueDeserializer)
       at Castle.Proxies.Consumer`2Proxy..ctor(IInterceptor[] , IEnumerable`1 , IDeserializer`1 , IDeserializer`1 )
    at FakeItEasy.Core.DefaultExceptionThrower.ThrowFailedToGenerateProxyWithArgumentsForConstructor(Type typeOfFake, String reasonForFailure)
    at FakeItEasy.Creation.FakeObjectCreator.AssertThatProxyWasGeneratedWhenArgumentsForConstructorAreSpecified(Type typeOfFake, ProxyGeneratorResult result, IProxyOptions proxyOptions)
    at FakeItEasy.Creation.FakeObjectCreator.CreateFake(Type typeOfFake, IProxyOptions proxyOptions, DummyCreationSession session, IDummyValueResolver resolver, Boolean throwOnFailure)
    at FakeItEasy.Creation.DefaultFakeAndDummyManager.CreateFake(Type typeOfFake, Action`1 optionsBuilder)
    at FakeItEasy.A.Fake[T](Action`1 optionsBuilder)
    Kafka.cs(14,0): at FakeItEasyQuestions2015.Kafka.MakeConsumer()

您可以看到fakeiteasy本身在调用 Consumer 类的构造函数:

An exception of type System.ArgumentException was caught during this call. Its message was:
    'group.id' configuration parameter is required and was not specified.

这是从第756行的使用者构造函数中抛出的:

if (config.FirstOrDefault(prop => string.Equals(prop.Key, "group.id", StringComparison.Ordinal)).Value == null)
{
    throw new ArgumentException("'group.id' configuration parameter is required and was not specified.");
}

看来

Consumer(IEnumerable<KeyValuePair<string, object>> config,
         IDeserializer<TKey> keyDeserializer,
         IDeserializer<TValue> valueDeserializer)`

对其输入有一些未满足的要求。特别是,它似乎需要 config 包含一个键为“group.id”的元素。如果我把你的密码改成

var fakeconsumer = A.Fake<Consumer<Null, string>>(
    (x => x.WithArgumentsForConstructor(
        () => new Consumer<Null, string>(new [] { new KeyValuePair<string, object>("group.id", "hippo")},
        A.Dummy<IDeserializer<Null>>(),
        A.Dummy<StringDeserializer>()))));

假货被制造出来了。
我注意到你交叉投递到了1176期的假货。我会在那里做个笔记,然后到这里来得到这个答案。

gywdnpxw

gywdnpxw2#

我不是Kafka消费类的Maven,但看起来你调用它的方式是:

Consumer<Null, string>

但我在代码中能找到的构造函数只有:

public Consumer(
         IEnumerable<KeyValuePair<string, object>> config)

public Consumer(
            IEnumerable<KeyValuePair<string, object>> config,
            IDeserializer<TKey> keyDeserializer,
            IDeserializer<TValue> valueDeserializer)

所以没有对手。看起来您想使用第一个,因此缺少了ienumerable部分。

相关问题