我使用F#和Foq为C#项目编写单元测试。
我正在尝试设置一个具有out参数的接口的模拟,我甚至不知道如何启动。这可能与代码引号有关,但我的理解到此为止。
接口如下:
public interface IGetTypeNameString
{
bool For(Type type, out string typeName);
}在C#接口的使用中,如下所示:
[Fact]
public void Foq_Out()
{
// Arrange
var name = "result";
var instance = new Mock<IGetTypeNameString>()
.Setup(x => x.For(It.IsAny<Type>(), out name))
.Returns(true)
.Create();
// Act
string resultName;
var result = instance.For(typeof(string), out resultName);
// Assert
Assert.True(result);
Assert.Equal("result", resultName);
}至于如何用F#实现这一点,我完全迷失了方向。我尝试了一些类似的东西
let name = "result"
let instance = Mock<IGetTypeNameString>().Setup(<@ x.For(It.IsAny<Type>(), name) @>).Returns(true).Create();的错误消息对引号表达式加下划线。
This expression was expected to have type IGetTypeNameString -> Quotations.Expr<'a> but here has type Quotations.Expr<'b>没有任何指示类型a和b应该是,我不知道如何纠正这一点。
:?>
(当我使用open Foq.Linq时,它变得更疯狂了;然后错误列表窗口开始告诉我Action<'TAbstract> -> ActionBuilder<'TAbstract>之类的东西可能会超载,我甚至会.)
任何帮助或解释都非常感谢!
编辑:
因此,正如所述的这里,byref/out参数不能用于代码引号。这能在F#中建立起来吗?
发布于 2014-02-03 07:14:32
Foq支持使用C#命名空间从C#中设置Foq.Linq out参数。
可以通过IGetTypeNameString轻松地在F#中设置对象表达式接口。
let mock =
{ new IGetTypeNameString with
member __.For(t,name) =
name <- "Name"
true
}对于在F#中没有模拟的声明,如C#的受保护成员和out参数,您还可以使用SetupByName重载,即:
let mock =
Mock<IGetTypeNameString>()
.SetupByName("For").Returns(true)
.Create()
let success, _ = mock.For(typeof<int>)https://stackoverflow.com/questions/21517897
复制相似问题