Moq 匿名类型的函数

编程入门 行业动态 更新时间:2024-10-12 01:30:50
本文介绍了Moq 匿名类型的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我正在尝试模拟此方法

Task<TResult> GetResultAsync<TResult>(Func<string, TResult> transformFunc)

喜欢这个

iMock.Setup(m => m.GetResultAsync(It.IsAny<Func<string, object>>())).ReturnsAsync(new { isPair = false });

测试调用将匿名类型传递给泛型参数的方法,如下所示

The method to test doing the call passing an anonymous type to the generic parameter like this

instance.GetResultAsync(u => new {isPair = u == "something" }) //dont look at the function return because as generic could have diferent implementations in many case

Moq 永远不会将我的 GetResultAsync 方法与发送的参数相匹配.

Moq never matches my GetResultAsync method with the parameters sent.

我使用的是 Moq 4

I'm using Moq 4

推荐答案

匿名类型会给您带来问题.你需要一个具体的类型才能工作.

The anonymous type is going to cause you problems. You need a concrete type for this to work.

以下示例在我更改时有效

The following example worked when I changed

instance.GetResultAsync(u => new {isPair = u == "something" })

instance.GetResultAsync(u => (object) new {isPair = u == "something" })

Moq 无法匹配匿名类型,这就是调用时得到 null 的原因.

Moq is unable to match the anonymous type and that is why you get null when called.

[TestClass]
public class MoqUnitTest {
    [TestMethod]
    public async Task Moq_Function_With_Anonymous_Type() {
        //Arrange
        var expected = new { isPair = false };

        var iMock = new Mock<IService>();
        iMock.Setup(m => m.GetResultAsync(It.IsAny<Func<string, object>>()))
            .ReturnsAsync(expected);

        var consumer = new Consumer(iMock.Object);

        //Act   
        var actual = await consumer.Act();

        //Assert
        Assert.AreEqual(expected, actual);
    }

    public interface IService {
        Task<TResult> GetResultAsync<TResult>(Func<string, TResult> transformFunc);
    }

    public class Consumer {
        private IService instance;

        public Consumer(IService service) {
            this.instance = service;
        }

        public async Task<object> Act() {
            var result = await instance.GetResultAsync(u => (object)new { isPair = u == "something" });
            return result;
        }
    }
}

如果调用 GetResultAsync 的代码依赖于使用匿名类型,那么您尝试对测试执行的操作不适用于当前设置.您可能需要为该方法提供具体类型.

if the code calling the GetResultAsync is dependent on using the anonymous type then what you are trying to do with your test wont work with your current setup. You would probably need to provide a concrete type to the method.

[TestClass]
public class MoqUnitTest {

    [TestMethod]
    public async Task Moq_Function_With_Concrete_Type() {
        //Arrange
        var expected = new ConcreteType { isPair = false };

        var iMock = new Mock<IService>();
        iMock.Setup(m => m.GetResultAsync(It.IsAny<Func<string, ConcreteType>>()))
            .ReturnsAsync(expected);

        var sut = new SystemUnderTest(iMock.Object);

        //Act   
        var actual = await sut.MethodUnderTest();

        //Assert
        Assert.AreEqual(expected, actual);
    }

    class ConcreteType {
        public bool isPair { get; set; }
    }

    public interface IService {
        Task<TResult> GetResultAsync<TResult>(Func<string, TResult> transformFunc);
    }

    public class SystemUnderTest {
        private IService instance;

        public SystemUnderTest(IService service) {
            this.instance = service;
        }

        public async Task<object> MethodUnderTest() {
            var result = await instance.GetResultAsync(u => new ConcreteType { isPair = u == "something" });
            return result;
        }
    }
}

这篇关于Moq 匿名类型的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-20 20:26:01,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/988659.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:函数   类型   Moq

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!