- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
谁能给我解释一下,为什么这行不通?
builder.Setup(b => b.BuildCommand(query ?? It.IsAny<string>())).Returns(command);
如果query
是null
, BuildCommand
将通过 null
,而不是 It.IsAny<string>()
相反,我必须这样做:
if(query == null)
builder.Setup(b => b.BuildCommand(It.IsAny<string>())).Returns(command);
else
builder.Setup(b => b.BuildCommand(query)).Returns(command);
和delegate有关系吗?
编辑 - 完整示例
public static void ReturnFromBuildCommand(this Mock<IQueryCommandBuilder> builder, IQueryCommand command, string query = null)
{
if(query == null)
builder.Setup(b => b.BuildCommand(It.IsAny<string>())).Returns(command);
else
builder.Setup(b => b.BuildCommand(query)).Returns(command);
}
然后我可以这样调用它
var command = new Mock<IQueryCommand>();
var builder = new Mock<IQueryCommandBuilder>();
builder.ReturnFromBuildCommand(command.Object);
或者
string query = "SELECT Name FROM Persons;";
builder.ReturnFromBuildCommand(command.Object, query);
取决于我是否关心参数。
最佳答案
Setup
mock 的方法接受一个表达式,然后 Moq 框架解构该表达式以确定被调用的方法及其参数。然后它设置一个拦截器来匹配参数。
您可以在 Mock
中看到它来源:
internal static MethodCallReturn<T, TResult> Setup<T, TResult>(
Mock<T> mock,
Expression<Func<T, TResult>> expression,
Condition condition)
where T : class
{
return PexProtector.Invoke(() =>
{
if (expression.IsProperty())
{
return SetupGet(mock, expression, condition);
}
var methodCall = expression.GetCallInfo(mock);
var method = methodCall.Method;
var args = methodCall.Arguments.ToArray();
ThrowIfNotMember(expression, method);
ThrowIfCantOverride(expression, method);
var call = new MethodCallReturn<T, TResult>(mock, condition, expression, method, args);
var targetInterceptor = GetInterceptor(methodCall.Object, mock);
targetInterceptor.AddCall(call, SetupKind.Other);
return call;
});
}
在这里args
类型为 Expression[]
.
(引用:https://github.com/moq/moq4/blob/master/Source/Mock.cs#L463)
这args
数组被传递到 Moq 类型的构造函数中 MethodCallReturn
作为参数 arguments
.该构造函数(通过基类 MethodCall
)使用 MatcherFactory.Create
生成参数匹配器. (引用:https://github.com/moq/moq4/blob/master/Source/MethodCall.cs#L148)
事情开始变得有趣了!在 MatcherFactory.Create 方法中,它试图通过查看 Expression.NodeType
来确定参数表达式的类型。和/或根据已知类型检查它,例如 MatchExpression
(这就是 Is.Any<string>()
之类的东西)。(引用:https://github.com/moq/moq4/blob/master/Source/MatcherFactory.cs#L54)
让我们退后一步。在您的具体情况下,代码 query ?? Is.Any<string>()
被编译成一个表达式本身——就像这个丑陋的困惑(由 dotPeek 反编译器生成):
(Expression) Expression.Coalesce((Expression) Expression.Field((Expression) Expression.Constant((object) cDisplayClass00, typeof (Extension.\u003C\u003Ec__DisplayClass0_0)),
FieldInfo.GetFieldFromHandle(__fieldref (Extension.\u003C\u003Ec__DisplayClass0_0.query))),
(Expression) Expression.Call((Expression) null, (MethodInfo) MethodBase.GetMethodFromHandle(__methodref (It.IsAny)), new Expression[0]))
这就是第一个参数的样子。您可以重写代码以更好地表达 Moq 看到的内容,如下所示:
public static void ReturnFromBuildCommand(this Mock<IQueryCommandBuilder> builder, IQueryCommand command, string query = null)
{
Expression<Func<IQueryCommandBuilder, IQueryCommand>> expressOfFunc = commandBuilder => (commandBuilder.BuildCommand(query ?? It.IsAny<string>()));
var methodCall = expressOfFunc.Body as MethodCallExpression;
var args = methodCall.Arguments.ToArray();
var nodeType = args[0].NodeType;
builder.Setup(expressOfFunc)
.Returns(command);
}
如果下断点,可以看到nodeType
的值是Coalesce
.现在,返回并将其更改为仅使用 query
, 和 nodeType
变成 MemberAccess
.使用 It.IsAny<string>()
, 和 nodeType
是Call
.
这解释了这三种方法之间的差异,以及为什么它不像您预期的那样起作用。至于为什么会触发null
老实说,我不清楚,但是无论匹配器来自 MatcherFactory.CreateMatcher
好像觉得null
是您的模拟配置的有效值。
关于c# - 为什么是??运算符(operator)不在最小起订量设置方法中工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44942162/
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 这个问题似乎离题,因为它缺乏足够的信息来诊断问题。 更详细地描述您的问题或include a minim
家庭作业 必须同时重载 operator>(istream &in, Complex &value); public: // constructor Compl
这个问题在这里已经有了答案: What are the basic rules and idioms for operator overloading? (8 个答案) 关闭 5 年前。 有什么区别
这是文档中一个不清楚的示例,使用此运算符:http://package.elm-lang.org/packages/elm-lang/core/3.0.0/Json-Decode#at 最佳答案 请注
我不明白这种行为: > sort([1,2,3,4]) ~~ sort([1,2,3,4]) False 你能给我解释一下吗? 为什么这两个列表(显然是相等的)根据 Perl 6 不相等。 更新 有趣
我正在尝试将 Ø 设为逻辑否定运算符。 ¬ True; multi sub prefix: ($n) { return not $n; } 当我运行上面的程序时,它返回以下错误: $
class Port { private: char * brand; char style[20]; // i.e., tawny, ruby, vintage int bo
早上好。我有一些问题。我有这些字段: name: "Mike", city: "NY", address: "something", pets: ["dog", "cat"] 我创建了索引 db.pe
我有以下代码使用 Javascript Webcrypto-API 解密 AES 加密数据,但它会导致“OperationError”并显示消息“操作因操作特定原因而失败”: function loa
我制作了一个自定义 Airflow 操作符,这个操作符接受一个输入,这个操作符的输出在 XCOM 上。 我想要实现的是使用一些定义的输入调用运算符,将输出解析为可在分支运算符内部调用的 Python,
int a = 1; a += ++a; cout << a << endl; // 4 int a = 1; a += a++; cout << a << endl; // 3 为什么这两个例子有不
我有这个队列的实现: #include using namespace std; template struct elem_q { T inf; elem_q* link; }; template
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 1
我对 C++ 中的运算符重载有疑问。我有代表复数的结构,我正在重载运算符,所以我可以用复数进行计算。Visual Studio 2012 给我这个错误:1 IntelliSense:没有运算符“!=”
我正在尝试创建一个 BoolArray 类,它表示一个 bool 值数组,而不是为每个 bool 变量保存 1 个 bool 值。它使用 1 个字符变量来表示使用位的 8 个 bool 值,并节省内存
我类有 2 个运算符有点问题。 我的类(class)宣布: template class MyMap{ keyType keys[MAX]; valueType values[MAX
我有类的迭代器模板和用于 for 语句的类。 template class Itr2 { public: Itr2() { } ~Itr2() { } typedef t
Section 7.2 Enumeration declarations 没有说明任何关于 operator!=() 和作用域的 operator==()枚举。但是下面的代码可以编译。 #includ
我需要将一个 int 序列化到本地文件并将其读入内存。这是代码 #include "stdafx.h" #include #include using namespace std; int _tm
operator += 这样定义对吗?! void operator +=(const BigNumber& other) { *this=(*this) + other; } 在这样的类中:
我是一名优秀的程序员,十分优秀!