- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我有一个单元测试,我使用 .Returns() 返回一些示例数据:
[TestMethod]
public void TestRetrieveElementsInVersion()
{
IRetrieveElementSequence component = Substitute.For<IRetrieveElementSequence>();
List<UnconstructedElement> list = new List<UnconstructedElement>
{
new UnconstructedElement{Version = "1"},
new UnconstructedElement{Version = "2"}
};
component.RetrieveElements().Returns(list); // exception reported here
const string target = "1";
IRetrieveElementSequence service = new RetrieveElementsInAVersion(component, target);
IList<UnconstructedElement> result = service.RetrieveElements();
bool check = result.All(e => e.Version == target);
Assert.IsTrue(check);
}
当单独运行测试时,此代码使用 ReSharper 运行器在 Visual Studio 中通过。当它作为列表的一部分运行时失败,例如我从解决方案运行所有测试时。
NSubstitute.Exceptions.UnexpectedArgumentMatcherException:参数匹配器(Arg.Is,Arg.Any)应该只用于代替成员参数。不要在 Returns() 语句或成员调用之外的任何其他地方使用。
我什至没有看到我在哪里使用 Arg.Any 或 Arg.Is。我在做什么让 NSubstitute 提示?当我使用 .Returns() 返回非本地对象列表时会发生这种情况。
最佳答案
这很可能是由于先前针对非虚拟方法或在 Returns
语句中使用参数匹配器进行的测试。
不幸的是,这可能很难调试。第一步是查看当您运行此夹具中的所有测试时是否出现问题。如果是这样,请检查该夹具中 Arg.Is|Any
的所有使用,从测试失败之前立即运行的那个开始(如果您的测试框架使用可预测的测试顺序,否则您将需要查看测试日志以查看哪些测试继续失败的测试)。
如果该 fixture 没有发生,您将需要查看预先运行的 fixture 以查看剩余的 arg 匹配器来自何处。它很可能在失败测试附近的某个地方。
编辑 2021-03-28:NSubstitute.Analyzers package 可以帮助在编译时发现这些问题。我强烈建议将它添加到任何包含 NSubstitute 的测试项目中。
关于c# - NSubstitute 测试本身有效,但会在套件中抛出意外的匹配器参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40922774/
我有一个用“NSubstitute”模拟的接口(interface),它包含返回 concreate 类的属性,即返回值不是接口(interface)。例如 public interface ISom
我有一个用“NSubstitute”模拟的接口(interface),其中包含返回 concreate 类的属性,即返回值不是接口(interface)。例如 public interface ISo
我有一个用“NSubstitute”模拟的接口(interface),其中包含返回 concreate 类的属性,即返回值不是接口(interface)。例如 public interface ISo
我正在使用 NSubstitute 通过 PartsOf() 来模拟一个类方法(我需要一些方法来工作)。它看起来像这样: var mock = Substitute.ForPartsOf(); moc
使用 NSubstitute。对于某些测试,我想断言替代者没有收到任何调用。我可以对界面中的每个方法使用 DidNotReceiveWithAnyArgs(),但这很乏味而且不够健壮(如果将新方法添加
使用 NSubstitute。对于某些测试,我想断言替代者没有收到任何调用。我可以对界面中的每个方法使用 DidNotReceiveWithAnyArgs(),但这很乏味而且不够健壮(如果将新方法添加
我有一个用 NSubstitute 伪造的对象,它有一个被调用两次的方法。我想验证该方法实际上已被调用两次(且仅调用两次)。我浏览了文档和谷歌,但没有运气。任何帮助,将不胜感激。谢谢。 最佳答案 NS
void ABC() { var foo = Substitute.For(); foo.When(x => x.Bar()).Do(x => counter++); ....
我有一个看起来像这样的类(class): public class MyClass { public virtual bool A() { return 5 ();
我在使用 NSubstitute 模拟带有输出参数的方法时遇到过这种情况。我不确定如何最好地用文本解释它,所以我将使用一些人为的示例和测试用例...... 在这个人为的示例中,我将使用 IDictio
void ABC() { var foo = Substitute.For(); foo.When(x => x.Bar()).Do(x => counter++); ....
我有一个看起来像这样的类(class): public class MyClass { public virtual bool A() { return 5 ();
对于下面的代码,我得到了这个断言失败,不知道为什么: Assert.AreEqual failed. Expected:. Actual:. public interface IA { voi
我对 NSubstitute、模拟和单元测试总体来说是新手。 我正在尝试使用 NSubstitute 删除测试类中的一些依赖项,但模拟对象中的方法的行为并不符合我的预期(根据我的配置方式)。以下是我在
假设我有一个类: public abstract class Test { internal abstract int Prop { get; } } 现在,我
我在单元测试中创建了 Person 和 AddressBook 类的替代品。AddressBook 类包含 Person 类型和名称的属性:SamplePerson。 public interface
我有一个测试,其中 NSubstitute 检查假类中的错误调用。当我像下面的代码一样进行测试时,Received(...) 方法会检查值 factory.featureClassName 是否返回一
我有一个带有以下声明的接口(interface): void MapServiceMessages(IEnumerable serviceMessages, List responseMessages
大家好,我是 NSubstitute 框架的新手。我正在尝试测试我的一些类(class),但是当我使用 NSubstitute 检查收到的电话时,它说没有收到匹配的电话。 我正在尝试测试 Tick()
我正在使用 Nsubstitute 进行模拟。为了减少代码,我想编写一个伪造通用属性的通用类: public class Tester where TValue: IValue { /
我是一名优秀的程序员,十分优秀!