Telerik JustMock?下载30天免费试用

模拟密封类

此功能允许您伪造密封类及其成员的调用,设置期望并使用AAA原则。模拟密封类和调用它们的方法/属性不会影响你编写测试的方式,也就是说,模拟非密封类使用了相同的语法。

重要的

要使用密封嘲讽,首先需要进入提升模式通过从菜单中启用JustMock。学习如何在如何启用/禁用Telerik JustMock的话题。

此特性仅在商业版本的Telerik JustMock.指主题,以了解更多关于商业版本和免费版本之间的差异。

安排密封类上的方法调用

为了演示如何从密封类中模拟方法的行为,让我们以以下类为例:

示例设置

公共密封类FooSealed{公共int Echo(int arg1){返回arg1;}}
Public NotInheritable Class FooSealed Public Function Echo(arg1 As Integer) As Integer Return arg1 End Function结束类

示例1展示如何排列密封类的非虚方法。更具体地说,如何设置调用foo。回声方法。int参数应该返回10

例1:排列方法

[TestMethod] public void ShouldAssertFinalMethodCallOnASealedClass() {// Arrange var foo = Mock.Create();Mock.Arrange (() = > foo.Echo (Arg.IsAny < int > ())) .Returns (10);// Act var actual = foo.Echo(1);// Assert。AreEqual(10、实际);}
公共子ShouldAssertFinalMethodCallOnASealedClass() '安排Dim foo = Mock。创建(Of FooSealed)() Mock.Arrange(Function() foo.Echo(Arg.IsAny(Of Integer)())).Returns(10) ' Act Dim actual = foo.Echo(1) ' Assert Assert.AreEqual(10, actual) End Sub

为带有内部构造函数的密封类创建Mock

即使是密封类只有一个内部构造函数时,您仍然可以创建一个mock,调用它的成员并验证结果。

下面是用于演示的示例类:

示例设置

public密封类FooSealedInternal{内部FooSealedInternal() {} public int Echo(int arg1){返回arg1;}}
Public NotInheritable Class FooSealedInternal Friend Sub New() End Sub Public Function Echo(arg1 As Integer) As Integer Return arg1 End Function结束类

示例2设置了一个调用foo。回声方法。int参数应返回10。方法的返回值进行断言,以确保实际创建了对象foo。回声方法。

例2:创建带有内部构造函数的密封类的模拟

[TestMethod] public void ShouldCreateMockForASealedClassWithInternalConstructor() {// Arrange var foo = Mock.Create();Mock.Arrange (() = > foo.Echo (Arg.IsAny < int > ())) .Returns (10);// Assert. isnotnull (foo);// Act var actual = foo.Echo(1);// Assert。AreEqual(10、实际);}
公共子ShouldCreateMockForASealedClassWithInternalConstructor() '安排Dim foo = Mock。创建(Of FooSealedInternal)() Mock.Arrange(Function() foo.Echo(Arg.IsAny(Of Integer)())).Returns(10) ' Assert Assert.IsNotNull(foo) ' Act Dim actual = foo.Echo(1) ' Assert Assert.AreEqual(10, actual) End Sub

为带有接口的密封类创建模拟

您还可以模拟实现接口的密封类。

示例设置

公共接口IFoo {void Execute();void Execute(int arg1);}公共密封类Foo: IFoo{公共无效Execute(){抛出新的NotImplementedException();} void IFoo。Execute(int arg1){抛出新的NotImplementedException();}}
Public接口IFoo Sub Execute() Sub Execute(arg1 As Integer)结束接口Public非继承类Foo实现IFoo Public Sub Execute()实现IFoo。Execute Throw New NotImplementedException() End Sub Private Sub Execute(arg1 As Integer)实现IFoo。Execute Throw New NotImplementedException() End子结束类

创建模拟时使用模拟。创建,它的所有依赖关系也是在幕后实现的。在示例3,我们嘲笑喷火类,它实现了IFoo接口。

例3:从实现接口的密封类创建mock,并控制其方法行为

[TestMethod] public void ShouldAssertCallOnVoid() {// Arrange var foo = Mock.Create< foo >();Bool called = false;//当foo.Execute()被调用时,跳过它的实现并将called设置为true。Mock.Arrange(() => foo.Execute()).DoInstead(() => called = true);//执行foo.Execute();// Assert. istrue(被调用);}
 Public Sub ShouldAssertCallOnVoid() '安排Dim foo = Mock。创建(Of Foo)() Dim called As Boolean = False ' When foo.Execute() is invoked, skip its implementation and set called to true instead. Mock.Arrange(Sub() foo.Execute()).DoInstead(Sub() called = True) ' Act foo.Execute() ' Assert Assert.IsTrue(called) End Sub

此外,如果你有兴趣IFoo接口实现,可以使用作为运算符并调用接口成员,如下所示。

例4:从实现接口的密封类创建mock,并验证接口方法的行为

[TestMethod] public void ShouldAssertCallOnVoidThroughAnInterface() {// Arrange var foo = Mock.Create< foo >();Bool called = false;//当foo.Execute()被调用时,跳过它的实现并将called设置为true。Mock.Arrange(() => foo.Execute()).DoInstead(() => called = true);// Act IFoo IFoo = foo;iFoo.Execute ();// Assert. istrue(被调用);}
 Public Sub ShouldAssertCallOnVoidThroughAnInterface() '安排Dim foo = Mock。创建(Of Foo)() Dim called As Boolean = False ' When foo.Execute() is invoked, skip its implementation and set called to true instead. Mock.Arrange(Sub() foo.Execute()).DoInstead(Sub() called = True) ' Act Dim iFoo As IFoo = foo iFoo.Execute() ' Assert Assert.IsTrue(called) End Sub

另请参阅

在本文中
Baidu
map