- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想编写一个测试来检查我的抽象类构造函数是否正确处理了无效参数。我写了一个测试:
[TestMethod]
[ExpectedException(typeof(ArgumentException))]
public void MyClassCtorTest()
{
var dummy = Substitute.For<MyClass>("invalid-parameter");
}
这个测试没有通过,因为 NSubstitute 抛出一个 TargetInvocationException
而不是 ArgumentException
。我寻找的实际异常实际上是 TargetInvocationException
的 InnerException
。我可以编写一个辅助方法,例如:
internal static class Util {
public static void UnpackException(Action a) {
try {
a();
} catch (TargetInvocationException e) {
throw e.InnerException;
} catch (Exception) {
throw new InvalidOperationException("Invalid exception was thrown!");
}
}
}
但我想,应该有某种通用的方法来解决这个问题。有吗?
最佳答案
NSubstitute 目前没有解决这个问题的通用方法。
其他一些解决方法包括手动子类化抽象类以测试构造函数,或手动断言内部异常而不是使用 ExpectedException
。
例如,假设我们有一个需要非负整数的抽象类:
public abstract class MyClass {
protected MyClass(int i) {
if (i < 0) {
throw new ArgumentOutOfRangeException("i", "Must be >= 0");
}
}
// ... other members ...
}
我们可以在测试夹具中创建一个子类来测试基类构造函数:
[TestFixture]
public class SampleFixture {
private class TestMyClass : MyClass {
public TestMyClass(int i) : base(i) { }
// ... stub/no-op implementations of any abstract members ...
}
[Test]
[ExpectedException(typeof(ArgumentOutOfRangeException))]
public void TestInvalidConstructorArgUsingSubclass()
{
new TestMyClass(-5);
}
// Aside: I think `Assert.Throws` is preferred over `ExpectedException` now.
// See http://stackoverflow.com/a/15043731/906
}
或者,您仍然可以使用模拟框架并对内部异常进行断言。我认为这不如前一个选项好,因为我们深入研究 TargetInvocationException
的原因并不明显,但这里有一个示例:
[Test]
public void TestInvalidConstructorArg()
{
var ex = Assert.Throws<TargetInvocationException>(() => Substitute.For<MyClass>(-5));
Assert.That(ex.InnerException, Is.TypeOf(typeof(ArgumentOutOfRangeException)));
}
关于c# - NSubstitute 中的 TargetInvocationException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19361261/
我有一个用“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 { /
我是一名优秀的程序员,十分优秀!