简体   繁体   English

大众运输消费者难以测试

[英]MassTransit consumer hard to test

I am having 2 issues testing MassTransit consumers:我在测试 MassTransit 消费者时遇到了两个问题:

  1. Sync issue同步问题
  2. MessageData消息数据

The first one is like this:第一个是这样的:

var testConsumer = TestFactory.ForConsumer<ImageUploadConsumer>().New(
                test =>
                {
                    test.UseConsumerFactory(new InstanceConsumerFactory<ImageUploadConsumer>(ImageConsumer));                    
                    test.Publish(message, (scenario, context) =>
                    {

                    });                    
                });

            testConsumer.Execute(); //Is non blocking

The following line (below) fails, because this line:以下行(下方)失败,因为这一行:

        moqFileMetaRepo.Verify(_ => _.Add(It.IsAny<IFileMeta>()),Times.Once ); 

is executed 9.9/10 before... this line ever did:在 9.9/10 之前执行...这行曾经做过:

    public async Task Consume(ConsumeContext<ImageUploadWithThumb> context)

My fix has been to do我的解决办法是做

moqFileMetaRepo
            .Setup(repo => repo.Add(It.IsAny<IFileMeta>()))
            .Callback(() => { AutoEvent.Set(); });

And call the following before the assert:并在断言之前调用以下内容:

AutoEvent.WaitOne(TimeSpan.FromSeconds(10));

Which is really a lot of work.这真的是很多工作。 And makes TDD or Testing in general a hassle, which I fear is only going to get ignored over time.并且使 TDD 或测试总体上变得很麻烦,我担心随着时间的推移它只会被忽略。

MessageData issue is another one. MessageData 问题是另一个问题。 Here's the payload I'm sending through这是我发送的有效载荷

        message = new ImageUploadWithThumb()
        {
            Id = Guid.NewGuid(),
            FileName = "Test.jpg",
            User = "Me",
            Extension = "jpg",
            OriginalImage = new ConstantMessageData<byte[]>(new Uri("https://g00gle.com"), new byte[] { 1, 2, 3 })
        };

I'm expecting to get byte[] { 1, 2, 3 } on the other end without having to resort to creating an actual persistence.我期待在另一端获得byte[] { 1, 2, 3 } ,而不必求助于创建实际的持久性。

Instead:反而: 出错了

On the sender side the MessageData.Value resolves ok.在发送方,MessageData.Value 解析正常。 The consumer totally bombs.消费者完全炸弹。 Works in prod though =_= which is not where testing should be.虽然在生产中工作 =_= 这不是测试应该在的地方。

I really just want to mock and UnitTest my consumer w/o having to wrestle with the framework - preferably in under 5 mins or so.我真的只想模拟和 UnitTest 我的消费者不必与框架搏斗 - 最好在 5 分钟左右。 Is there a way out while sticking to MT3?坚持MT3有出路吗?

I would suggest looking at the MassTransit.TestFramework package.我建议查看MassTransit.TestFramework包。 It does require NUnit, but you could always take the classes and port it to your own test framework.它确实需要 NUnit,但您始终可以使用这些类并将其移植到您自己的测试框架中。

All of the MassTransit unit tests are written using the fixtures in this framework.所有 MassTransit 单元测试都是使用此框架中的夹具编写的。 The original .Testing namespace is in a world of hurt right not, it didn't survive completely and I'm unsure it's actually working completely.最初的 .Testing 命名空间是在一个受伤的世界里,它没有完全存活下来,我不确定它是否真的完全正常工作。 It wasn't designed for async, so it was difficult to transition without trashing it entirely.它不是为异步设计的,因此很难在不完全破坏它的情况下进行转换。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM