断言发生
Occurrence与连用模拟。断言
和模拟。AssertSet
以确定调用发生了多少次。
有6种类型的发生:
- Occurs.Never ()-指定一个特定的调用永远不会在模拟上进行。
- Occurs.Once ()-指定在模拟上只发生一次调用。
- Occurs.AtLeastOnce ()-指定在模拟上至少发生过一次调用。
- Occurs.AtLeast (numberOfTimes)-指定一个调用在模拟中应该发生的最少次数。
- Occurs.AtMost (numberOfTimes)-指定一个调用应该在模拟上发生的最多次数。
- Occurs.Exactly (numberOfTimes)-精确指定一个调用应该在模拟上发生的次数。
此外,您可以在方法的安排中直接设置发生次数。
你可以使用以下5种不同的construct:
- 发生(numberOfTimes)-指定一个调用应该在模拟上发生的确切次数。
- OccursOnce ()-指定一个调用应该只在模拟上发生一次。
- OccursNever ()-指定一个特定的调用不应该在mock上进行。
- OccursAtLeast (numberOfTimes)-指定一个调用应该在模拟中至少发生一次。
- OccursAtMost (numberOfTimes)-指定一个调用在模拟中最多应该发生的次数。
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
我们的约定是打电话回声
方法1
,2
或3.
作为参数。在断言中,我们使用匹配器来覆盖所有调用整数作为参数。与调用foo。回声
与1
,2
和3.
在代理阶段应用该匹配器,调用整数发生完全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)结束接口
这里我们定义了IUserValidationService
和IShoppingCartService
万博体育手机版网址我们将在下面的测试中断言其调用顺序的服务:
[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使您能够确保只有在先前调用了一个或多个先决条件之后才能调用方法。要实现这个用例,下面的工作流程是适用的:
- 安排方法来定义先决条件。
- 中指定所有必需先决条件的列表
毕竟
. 断言目标模拟,以验证是否调用了所有预期的先决条件。
下面是一个典型的例子
毕竟()
在行动。假设你有以下接口:公共接口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