Telerik JustMock免费下载30天试用版

断言发生

Occurrence与连用模拟。断言模拟。AssertSet以确定调用发生了多少次。

有6种类型的发生:

此外,您可以在方法的安排中直接设置发生次数。

你可以使用以下5种不同的construct:

JustMock还允许您验证调用顺序,使用:

  • 属于()-精确指定调用应该在模拟上发生的顺序。

除此之外,JustMock还允许您验证方法调用的先决条件是否得到满足

在下面的例子中,我们将使用下面的接口进行测试:

公共接口IFoo{无效提交();int Echo(int intar);}
Public Interface IFoo Sub Submit() Function Echo(intArg As Integer) As Integer结束接口

发生。从来没有

发生。从来没有用于验证在测试执行期间未调用方法/属性。

[TestMethod] public void ShouldOccursNever(){//安排var foo = Mock.Create();//Assert Mock.Assert(() => foo.Submit(), Occurs.Never());}
 Public Sub ShouldOccursNever() '安排Dim foo = Mock。Create(Of ifo)() 'Assert Mock.Assert(Sub() foo.Submit(), Occurs.Never()) End Sub

在上面的示例中,我们验证了这一点foo.Submit ()从来没有叫过。

发生。一次

发生。一次用于验证在测试执行期间是否只调用了一次方法/属性。

[TestMethod] public void ShouldOccursOnce(){//安排var foo = Mock.Create();/ /行动foo.Submit ();//Assert Mock.Assert(() => foo.Submit(), Occurs.Once());}
 Public Sub ShouldOccursOnce() '安排Dim foo = Mock。Create(Of ifo)() '行动起来。提交()Assert Mock.Assert(Sub() foo.Submit(), Occurs.Once())结束Sub

在上面的示例中,我们验证了这一点foo.Submit ()曾被召唤过一次。

发生。至少一次

发生。至少一次用于验证在测试执行期间至少调用了一次方法/属性。

[TestMethod] public void ShouldOccursAtLeastOnce(){//安排var foo = Mock.Create();/ /行动foo.Submit ();//Assert Mock.Assert(() => foo.Submit(), Occurs.AtLeastOnce());//多次调用Submit foo.Submit();foo.Submit ();// Assert -应该再次通过Mock.Assert(() => foo.Submit(), Occurs.AtLeastOnce());}
 Public Sub ShouldOccursAtLeastOnce() '安排Dim foo = Mock。Create(Of ifo)() '行动起来。提交()Assert Mock.Assert(Sub() foo.Submit(), Occurs.AtLeastOnce()) '多次调用Submit() foo.Submit() foo.Submit() 'Assert -应该再次通过Mock.Assert(Sub() foo.Submit(), Occurs.AtLeastOnce())结束Sub

上面的示例显示发生。至少一次只要调用该方法一次或多次,就满足要求。

Occurs.AtLeast (numberOfTimes)

发生。至少您指定至少需要验证在测试执行期间调用给定方法/属性的次数。

下一个例子使用了NUnit测试框架。

[TestMethod] public void shouldoccursatleastcoursenumberoftimes(){//安排var foo = Mock.Create();/ /行动foo.Submit ();foo.Submit ();//Assert NUnit.Framework.Assert.Throws(() => Mock.Assert(() => foo.Submit(), Occurs.AtLeast(3)));foo.Submit ();Mock.Assert(() => foo.Submit(), Occurs.AtLeast(3));}
 Public Sub shouldoccursatleastsurenumberoftimes () '安排Dim foo = Mock。Create(Of ifo)() '操作foo.Submit() foo.Submit() 'Assert - foo。提交was called only twice so this throws an exception NUnit.Framework.Assert.Throws(Of AssertFailedException)(Sub() Mock.Assert(Sub() foo.Submit(), Occurs.AtLeast(3))) ' Act - now, foo.Submit is called 3 times foo.Submit() ' Assert - that one passes Mock.Assert(Sub() foo.Submit(), Occurs.AtLeast(3)) End Sub

在上面的例子中,第一个断言方法抛出异常foo。提交方法在那时只被调用了两次,但是发生。至少要求调用该方法至少3次。之后,再次调用该方法,从而导致第二个方法中的条件断言要满足。

Occurs.AtMost (numberOfTimes)

发生。AtMost您最多可以指定在测试执行期间需要验证调用给定方法/属性的次数。

[TestMethod] [ExpectedException(typeof(AssertFailedException))] public void ShouldOccursCertainNumberOfTimesAtMost(){//安排var foo = Mock.Create();/ /行动foo.Submit ();foo.Submit ();//Assert Mock.Assert(() => foo.Submit(), Occurs.AtMost(2));//行动-现在我们再次调用Submit -总共3次foo.Submit();// Assert -抛出异常Mock.Assert(()=>foo.Submit(),Occurs.AtMost(2));}
  Public Sub ShouldOccursCertainNumberOfTimesAtMost() '安排Dim foo = Mock。Create(Of ifo)() '操作foo.Submit() foo.Submit() 'Assert Mock.Assert(Sub() foo.Submit(), Occurs.AtMost(2)) '行动-现在我们再次调用Submit -总共调用3次foo.Submit() 'Assert -抛出异常Mock.Assert(Sub() foo.Submit(), Occurs.AtMost(2)) End Sub

在上面的例子中,第一个断言作为foo。提交方法只被调用了两次。然而,第二个断言抛出异常,因为同一方法总共被调用了3次,而条件最多被调用2次。

Occurs.Exactly (numberOfTimes)

发生。完全您可以精确地指定在测试执行期间需要验证调用给定方法/属性的次数。

下一个例子使用了NUnit测试框架。

[TestMethod] public void ShouldOccursExactly(){//安排var foo = Mock.Create();//调用foo。提交twice foo.Submit(); foo.Submit(); //Assert - throws an exception - foo.Submit was called only twice NUnit.Framework.Assert.Throws(() => Mock.Assert(() => foo.Submit(), Occurs.Exactly(3))); // Act - call foo.Submit once again - 3 times in total foo.Submit(); // Assert - that should pass Mock.Assert(() => foo.Submit(), Occurs.Exactly(3)); // Act - call foo.Submit once again - 4 times in total foo.Submit(); // Assert - fails because foo.Submit was called more times than specified NUnit.Framework.Assert.Throws(() => Mock.Assert(() => foo.Submit(), Occurs.Exactly(3))); }
 Public Sub ShouldOccursExactly() '安排Dim foo = Mock。Create(Of ifo)() '行动-呼叫foo。提交twice foo.Submit() foo.Submit() ' Assert - throws an exception - foo.Submit was called only twice NUnit.Framework.Assert.Throws(Of AssertFailedException)(Sub() Mock.Assert(Sub() foo.Submit(), Occurs.Exactly(3))) ' Act - call foo.Submit once again - 3 times in total foo.Submit() ' Assert - that should pass Mock.Assert(Sub() foo.Submit(), Occurs.Exactly(3)) ' Act - call foo.Submit once again - 4 times in total foo.Submit() ' Assert - fails because foo.Submit was called more times than specified NUnit.Framework.Assert.Throws(Of AssertFailedException)(Sub() Mock.Assert(Sub() foo.Submit(), Occurs.Exactly(3))) End Sub

在上面的例子中,第一个断言抛出异常,因为我们调用foo。提交发生。完全指定条件。第二个断言通过,因为有3个调用foo。提交方法。第三个断言再次抛出异常,因为此时foo。提交方法被调用4次,这比在发生。完全条件。

发生(numberOfTimes)

发生(numberOfTimes)您可以精确地指定在测试执行期间需要验证调用给定方法/属性的次数。

下一个例子使用了NUnit测试框架。

[TestMethod] public void ShouldFailOnAssertAllWhenExpectionIsNotMet(){//安排var foo = Mock.Create();Mock.Arrange(() => foo.Submit()).Occurs(2);//断言NUnit.Framework.Assert.Throws(() => Mock.Assert(foo));}
 Public Sub ShouldFailOnAssertAllWhenExpectionIsNotMet() '安排Dim foo = Mock。Create(Of IFoo)() Mock.Arrange(Sub() foo.Submit()).Occurs(2) '断言NUnit.Framework.Assert。抛出(Of AssertFailedException)(Sub() Mock.Assert(foo))结束Sub

在上面的例子中,(2)发生标记调用需要执行2次,因此模拟。断言如果预期不当,就会失败。

OccursOnce ()

OccursOnce ()用于验证在测试执行期间是否只调用了一次方法/属性。

[TestMethod] public void ShouldArrangeOccursOnce(){//安排var foo = Mock.Create();Mock.Arrange(() => foo.Submit()).OccursOnce();//执行foo.Submit();// Assert Mock.Assert(foo);}
 Public Sub ShouldArrangeOccursOnce() '安排Dim foo = Mock。Create(Of IFoo)() Mock.Arrange(Sub() foo.Submit()).OccursOnce() '行动起来。提交()Assert Mock.Assert(foo)结束子句

在上面的例子中,我们这样安排foo.Submit ()必须只调用一次。

OccursNever ()

OccursNever ()用于验证在测试执行期间未调用方法/属性。

[TestMethod] public void ShouldArrangeOccursNever(){//安排var foo = Mock.Create();Mock.Arrange(() => foo.Submit()).OccursNever();// Assert Mock.Assert(foo);}
 Public Sub ShouldArrangeOccursNever() '安排Dim foo = Mock。Create(Of IFoo)() Mock.Arrange(Sub() foo.Submit()).OccursNever() 'Assert Mock.Assert(foo)结束子句

在上面的示例中,我们验证了这一点foo.Submit ()从来没有叫过。

OccursAtLeast (numberOfTimes)

OccursAtLeast (numberOfTimes)您指定至少需要验证在测试执行期间调用给定方法/属性的次数。

[TestMethod] public void shouldarrangeoccurs satleast(){//安排var foo = Mock.Create();Mock.Arrange(() => foo.Submit()). occurs least (2);//执行foo.Submit();foo.Submit ();foo.Submit ();// Assert Mock.Assert(foo);}
 Public Sub shouldarrangeoccurs least () '安排Dim foo = Mock。Create(Of IFoo)() Mock.Arrange(Sub() foo.Submit()). occurs least (2) '动作foo.Submit() foo.Submit() foo.Submit() 'Assert Mock.Assert(foo)结束子句

在上面的例子中,foo.Submit ()被称为3.次了。验证不会失败,因为它至少要执行2次了。

OccursAtMost (numberOfTimes)

OccursAtMost (numberOfTimes)您最多可以指定在测试执行期间需要验证调用给定方法/属性的次数。

[TestMethod] [ExpectedException(typeof(AssertFailedException))] public void shouldfailwheninvokedmorethanrequired(){//安排var foo = Mock.Create();Mock.Arrange(() => foo.Submit()). occurs most (2);//执行foo.Submit();foo.Submit ();foo.Submit ();}
  Public Sub shouldfailwheninvokedmorethanrequired () '安排Dim foo = Mock。Create(Of IFoo)() Mock.Arrange(Sub() foo.Submit()). occurrence (2) 'Act foo.Submit() foo.Submit() foo.Submit()结束Sub

在上面的示例中,我们指定了它foo.Submit ()最多只能调用2次。因为它被称为3次,AssertFailedException将在第三次调用后立即抛出。

断言多次出现

JustMock允许您使用Mock中的Matcher断言类似类型调用的多次出现。断言而不是分别断言它们中的每一个。

下面是一个例子:

[TestMethod] public void ShouldBeAbleToAssertOccursUsingMatcherForSimilarCallAtOneShot(){//安排var foo = Mock.Create();Mock.Arrange(() => foo.Echo(1))。返回((int arg) => arg);Mock.Arrange(() => foo.Echo(2))。返回((int arg) => arg);Mock.Arrange(() => foo.Echo(3))。返回((int arg) => arg);//动作foo.Echo(1);foo.Echo (2);foo.Echo (3);// Assert Mock.Assert(() => foo.Echo(Arg.AnyInt), Occurs.Exactly(3));}
 Public Sub ShouldBeAbleToAssertOccursUsingMatcherForSimilarCallAtOneShot() '安排Dim foo = Mock。Create(Of IFoo)() Mock.Arrange(Function() foo.Echo(1))。返回(Function(arg__1 As Integer) arg__1) Mock.Arrange(Function() foo.Echo(2))。返回(Function(arg__1 As Integer) arg__1) Mock.Arrange(Function() foo.Echo(3))。返回(Function(arg__1 As Integer) arg__1) '动作1 . echo (1) echo (2) echo (3)Assert Mock.Assert(Function() foo.Echo(Arg.AnyInt), Occurs.Exactly(3)) End Sub

我们的约定是打电话回声方法123.作为参数。在断言中,我们使用匹配器来覆盖所有调用整数作为参数。与调用foo。回声123.在代理阶段应用该匹配器,调用整数发生完全3.次了。指匹配器有关使用匹配器的更多信息。

验证呼叫顺序

要确保一组方法调用按特定顺序执行,可以使用属于()方法。要实现这一点,你需要使用以下工作流程:

  1. 您安排这些方法以定义它们的调用顺序。
  2. 你的行为。
  3. 对模拟进行断言,以检查方法的执行顺序是否符合预期。

    下面是一个例子属于()用例:

    [TestMethod] public void ShouldVerifyCallsOrder(){//安排var foo = Mock.Create();Mock.Arrange(() => foo.Submit()).InOrder();Mock.Arrange(() => foo.Echo(Arg.AnyInt)).InOrder();//执行foo.Submit();foo.Echo (5);// Assert Mock.Assert(foo);}
     Public Sub ShouldVerifyCallsOrder() '安排Dim foo = Mock。Create(Of IFoo)() Mock.Arrange(Sub() foo.Submit()).InOrder() Mock.Arrange(Sub() foo.Echo(Arg.AnyInt)).InOrder() 'Act foo.Submit() foo.Echo(5) 'Assert Mock.Assert(foo)结束子句

请注意有条不紊地进行Option还支持断言模拟调用的顺序,而不管测试范围内的实例是什么。想象一下,在应用程序中使用他/她的购物车之前,您必须验证用户已经登录。

公共接口IUserValidationService {int ValidateUser(字符串用户名,字符串密码);}公共接口IShoppingCartService {IList LoadCart(int userID);}
公共接口IUserValidationService Function ValidateUser(ByVal userName As String, ByVal password As String) As Integer结束接口IShoppingCartService Function LoadCart(ByVal userID As Integer) As IList(Of String)结束接口

这里我们定义了IUserValidationServiceIShoppingCartService万博体育手机版网址我们将在下面的测试中断言其调用顺序的服务:

[TestMethod] public void shouldassertinorderfordifferentinstancestestmethodscope () {string userName = "Bob";string password =“密码”;int userID = 5;var cart = new List {"Foo", "Bar"};//安排var userServiceMock = Mock.Create();var shoppingCartServiceMock = Mock.Create();Mock.Arrange(() => userServiceMock。ValidateUser(userName, password)).Returns(userID).InOrder().OccursOnce(); Mock.Arrange(() => shoppingCartServiceMock.LoadCart(userID)).Returns(cart).InOrder().OccursOnce(); // Act userServiceMock.ValidateUser(userName, password); shoppingCartServiceMock.LoadCart(userID); // Assert Mock.Assert(userServiceMock); Mock.Assert(shoppingCartServiceMock); }
 Public Sub shouldassertinorderfordifferentinstancestestmethodscope () Dim userName As String = "Bob" Dim password As String = " password " Dim userID As Integer = 5 Dim cart As IList(Of String) = {"Foo", "Bar"} ' Arrange Dim userServiceMock = Mock。Create(Of IUserValidationService)() Dim shoppingCartServiceMock = Mock。Create(Of IShoppingCartService)() Mock.Arrange(Function() userServiceMock。ValidateUser(userName, password)).Returns(userID).InOrder().OccursOnce() Mock.Arrange(Function() shoppingCartServiceMock.LoadCart(userID)).Returns(cart).InOrder().OccursOnce() ' Act userServiceMock.ValidateUser(userName, password) shoppingCartServiceMock.LoadCart(userID) ' Assert Mock.Assert(userServiceMock) Mock.Assert(shoppingCartServiceMock) End Sub

在排列相中我们定义了ValidateUser电话只应打一次,并在LoadCart服务调用。的LoadCart调用也应该只发生一次,并且应该遵循ValidateUser服务调用。我们采取行动,然后坚持我们的期望。

验证呼叫前提条件

JustMock使您能够确保只有在先前调用了一个或多个先决条件之后才能调用方法。要实现这个用例,下面的工作流程是适用的:

  1. 安排方法来定义先决条件。
  2. 中指定所有必需先决条件的列表毕竟
  3. 断言目标模拟,以验证是否调用了所有预期的先决条件。

    下面是一个典型的例子毕竟()在行动。假设你有以下接口:

    公共接口IBar {void Init();}公共接口IBarContainer {IEnumerable Elements {get;}}
    Public Interface IBarContainer ReadOnly Property Elements作为IEnumerable(Of IBarContainer)结束接口

    你需要验证一下FooContainer。元素毕竟是被称为Foo。初始化有过这样的经历,不如下面的测试能有所帮助:

    [TestMethod] public void shouldassertprerequisite () {// Arrange var barCollection = new List() {Mock.Create(), Mock.Create(), Mock.Create()};var barContainer = Mock.Create();Mock.Arrange(() => barContainer.Elements) . returnscollection (barCollection) . afterall (barCollection. elements)。选择(bar = > Mock.Arrange (() = > bar.Init ())) .ToArray ());//执行barCollection。ForEach(bar => bar. init ());var actualCollection = barContainer.Elements;// Assert Mock.Assert(barContainer);}
     Public Sub shouldassert先决条件()将Dim barCollection作为新列表(IBar的)从{Mock。Create(Of IBar)(), Mock。Create(Of IBar)(), Mock。Create(Of barbar)()} Dim barContainer = Mock。Create(Of IBarContainer)() Mock.Arrange(Function() barContainer.Elements) _ . returnscollection (barCollection) _ . afterall (barCollection. elements)Select(Function(bar As IBar) Mock.Arrange(Sub() bar. init ()) .ToArray()) 'barCollection行动。ForEach(Sub(bar As IBar) bar. init ()) Dim actualCollection = barContainer。元素的Assert Mock.Assert(barContainer
在本文中
Baidu
map