public void NotMet() { var myObject = Mock.Interface <IMyObject>(); Expect.Once.MethodCall(() => myObject.MyMethod(1)); var ex = Assert.Throws <ExpectationsException>(() => AssertExpectations.IsMetFor(myObject)); Assert.That(ex.Message, Is.StringStarting("All expectations has not been met, expected:")); }
public void TestShow() { //Arrange var foo = Mock.Interface <IFoo>(); Expect.Once.MethodCall(() => foo.Show(Any <MyObj> .Value.Matching(obj => obj.Attr == 1))); //Act var objectUnderTest = new ObjUnderTest(foo); //Assert AssertExpectations.IsMetFor(foo); }
public void ForSingleMock() { var myObject = Mock.Interface <IMyObject>(); Expect.Once.MethodCall(() => myObject.MyMethod(1)); Expect.Once.MethodCall(() => myObject.MyMethod(2)); myObject.MyMethod(1); myObject.MyMethod(2); AssertExpectations.IsMetFor(myObject); }
public void ForScope() { var expectationScope = new ExpectationScope(); var myObject1 = Mock.Interface <IMyObject>(expectationScope); var myObject2 = Mock.Interface <IMyObject>(expectationScope); Expect.Once.MethodCall(() => myObject1.MyMethod(1)); Expect.Once.MethodCall(() => myObject2.MyMethod(2)); myObject1.MyMethod(1); myObject2.MyMethod(2); AssertExpectations.IsMetFor(expectationScope); }
public void NumberOfCallsExceededButExceptionCatchedByUserCode() { var myObject = Mock.Interface <IMyObject>(); Expect.Once.MethodCall(() => myObject.MyMethod(1)); try { myObject.MyMethod(1); myObject.MyMethod(1); } catch (ExpectationsException) { } Assert.Throws <ExpectationsException>(() => AssertExpectations.IsMetFor(myObject)); }
public void MeetExpectationsInScope() { var expectationScope = new ExpectationScope(); var myObject1 = Mock.Interface <IMyObject>(expectationScope); var myObject2 = Mock.Interface <IMyObject>(expectationScope); Expect.Once.MethodCall(() => myObject1.MyMethod(1)); Expect.Once.MethodCall(() => myObject2.MyMethod(2)); using (expectationScope.BeginOrdered()) { Expect.AtLeastOnce.MethodCall(() => myObject1.MyMethod(3)); Expect.Between(0, 3).MethodCall(() => myObject1.MyGenericMethod("WILL PROBABLY NEVER BE INVOKED")); Expect.AtLeastOnce.MethodCall(() => myObject1.MyMethod(Any <int> .Value.Matching(i => i > 10))); using (expectationScope.BeginUnordered()) { Expect.AtLeastOnce.MethodCall(() => myObject2.MyMethod(4)); Expect.AtLeastOnce.MethodCall(() => myObject1.MyMethod(5)); } } myObject1.MyMethod(1); myObject2.MyMethod(2); myObject1.MyMethod(3); myObject1.MyMethod(3); myObject1.MyMethod(13); myObject1.MyMethod(12); myObject1.MyMethod(11); myObject1.MyMethod(5); myObject2.MyMethod(4); AssertExpectations.IsMetFor(expectationScope); }