从模拟方法返回模拟

编程入门 行业动态 更新时间:2024-10-28 02:24:13
本文介绍了从模拟方法返回模拟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我不确定该怎么做.

鉴于我有

public interface IFactory<T> where T : new() { IWrapper<T> GetT(string s); } public interface IWrapper<out T> where T : new() { void Execute(Action<T> action); }

当我这样做

public class MoqTest { public void test() { Mock<IWrapper<basicClass>> wrapperMock = new Mock<IWrapper<basicClass>>(); Mock<IFactory<basicClass>> factoryMock = new Mock<IFactory<basicClass>>() .Setup(p => p.GetT(It.IsAny<string>())) .Returns(wrapperMock.Object); } }

我明白了

无法隐式转换类型 Moq.Language.Flow.IReturnsResult<TestNamespace.IFactory<TestNamespace.basicClass>> 到Moq.Mock<TestNamespace.IFactory<TestNamespace.basicClass>>.一个 存在显式转换(您是否缺少演员表?)

Cannot implicitly convert type Moq.Language.Flow.IReturnsResult<TestNamespace.IFactory<TestNamespace.basicClass>> to Moq.Mock<TestNamespace.IFactory<TestNamespace.basicClass>>. An explicit conversion exists (are you missing a cast?)

请注意,这些只是模拟的示例对象.

Note that these are just mocked example objects.

似乎不认为返回类型等效.一个是IReturnResult,另一个是Moq.Mock

It seems it doesn't consider the return type equivalent. One being a IReturnResult, the other a Moq.Mock

推荐答案

您的问题基本上是,您将调用Returns方法的结果分配给factoryMock变量.

Your problem basically is that you are assigning the result of invoking the Returns method to the factoryMock variable.

您要先创建模拟并将其分配给factoryMock变量,然后按如下所示设置模拟:

You want to first create the mock and assign it to the factoryMock variable and then set the mock up like this:

Mock<IWrapper<basicClass>> wrapperMock = new Mock<IWrapper<basicClass>>(); Mock<IFactory<basicClass>> factoryMock = new Mock<IFactory<basicClass>>(); factoryMock .Setup(p => p.GetT(It.IsAny<string>())) .Returns(wrapperMock.Object);

更多推荐

从模拟方法返回模拟

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

发布评论

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

>www.elefans.com

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