public void MatchesCallCountWhenMatchingInvocation() { Matcher irrelevant = Is.Anything; BuildableExpectation expectation = (BuildableExpectation)BuildExpectation( "description", Is.AtLeast(0), Is.AtMost(4), receiver.MockObject, irrelevant, irrelevant, irrelevant, irrelevant); AssertIsActive(expectation, "should be active before any invocation"); Assert.IsTrue(expectation.Matches(invocation), "should match 1st invocation"); expectation.Perform(invocation); AssertIsActive(expectation, "should be active before 2nd invocation"); Assert.IsTrue(expectation.Matches(invocation), "should match 2nd invocation"); expectation.Perform(invocation); AssertIsActive(expectation, "should be active before 3rd invocation"); Assert.IsTrue(expectation.Matches(invocation), "should match 3rd invocation"); expectation.Perform(invocation); AssertIsActive(expectation, "should be active before 4th invocation"); Assert.IsTrue(expectation.Matches(invocation), "should match 4th invocation"); expectation.Perform(invocation); AssertIsNotActive(expectation, "should not be active after 4th invocation"); Assert.IsFalse(expectation.Matches(invocation), "should not match 5th invocation"); }
private void SetToMatcherTest(Mock <IParentInterface> mock) { Version version = new Version(1, 1, 1, 1); mock.Expects.One.SetProperty(_ => _.ReadWriteObjectProperty).To(Is.EqualTo(version)); mock.MockObject.ReadWriteObjectProperty = version; }
public void ChecksCallCountToAssertThatItHasBeenMet() { var irrelevant = Is.Anything; var mock = new ReflectiveInterceptor(null, null, "name", MockStyle.Default); var expectation = BuildExpectation( "description", Is.AtLeast(2), Is.AtMost(4), mock, irrelevant, irrelevant, irrelevant, irrelevant); AssertHasNotBeenMet(expectation, "should not have been met after no invocations"); expectation.Perform(invocation); AssertHasNotBeenMet(expectation, "should not have been met after 1 invocation"); expectation.Perform(invocation); AssertHasBeenMet(expectation, "should have been met after 2 invocations"); expectation.Perform(invocation); AssertHasBeenMet(expectation, "should have been met after 3 invocations"); expectation.Perform(invocation); AssertHasBeenMet(expectation, "should have been met after 4 invocations"); }
private void SetToValueTypeTest(Mock <IParentInterface> mock) { mock.Expects.One.SetProperty(_ => _.ReadWriteObjectProperty).To(Is.TypeOf <Version>()); mock.Expects.One.SetProperty(_ => _.IsReadOnly).To(Is.TypeOf <bool>()); mock.MockObject.ReadWriteObjectProperty = new Version(1, 2, 3, 4); mock.MockObject.IsReadOnly = true; }
public void IsTypeOfGeneric() { Matcher matcher = Is.TypeOf <Matcher>(); Assert.IsNotNull(matcher); Assert.IsTrue(typeof(TypeMatcher).IsInstanceOfType(matcher)); Assert.IsTrue(matcher.Matches(matcher)); }
public void VoidMethods() { _mock.Expects.One.Method(_ => _.MethodVoid()).Comment(""); _mock.Expects.One.Method(_ => _.MethodVoid <string>("10")).With(Is.StringContaining("10")).Comment(""); _mock.MockObject.MethodVoid(); _mock.MockObject.MethodVoid <string>("10"); }
public void ReturnsCloneOfPrototypeObject() { ICloneable prototype = ACloneableObject(); IAction action = new ReturnCloneAction(prototype); object result = ReturnActionTest.ResultOfAction(action); Verify.That(result, !Is.EqualTo(prototype)); }
public void MatchesObjectWithMatchingField() { ObjectWithFields o = new ObjectWithFields(); o.PublicField = "actual value"; Matcher m = new FieldMatcher("PublicField", Is.EqualTo("actual value")); Assert.IsTrue(m.Matches(o), "should match o"); }
public void DoesNotMatchIfValueMatchersDoNotMatchArgumentValues() { Matcher matcher = new ArgumentsMatcher(Is.EqualTo(arg1Value), Is.EqualTo(arg2Value)); Assert.IsFalse(matcher.Matches(InvocationWithArguments("other object", arg2Value)), "different first arg"); Assert.IsFalse(matcher.Matches(InvocationWithArguments(arg1Value, "other object")), "different second arg"); }
public void touchedWhenOneSpriteTouched() { var collection = new SpriteCollection(sprites, camera.MockObject, sensor.MockObject); sensor.Expects.AtLeastOne.Method(_ => _.insideSprite(null, null, null)) .With(Is.Anything, Is.EqualTo(sprite1.MockObject), Is.Anything).WillReturn(true); sensor.Expects.One.Method(_ => _.insideSprite(null, null, null)) .With(Is.Anything, Is.EqualTo(sprite2.MockObject), Is.Anything).WillReturn(false); Assert.That(collection.touchedAtSameTime(0f), Iz.False); }
public void MatchesObjectWithNamedPropertyAndMatchingPropertyValue() { ObjectWithProperties o = new ObjectWithProperties(); object aValue = new NamedObject("aValue"); o.A = aValue; Matcher m = new PropertyMatcher("A", Is.EqualTo(aValue)); Assert.IsTrue(m.Matches(o), "should match o"); }
public void StubsMatchArgumentsAndPerformActionsJustLikeAnExpectation() { Mock <IHelloWorld> helloWorld = Mocks.CreateMock <IHelloWorld>(); helloWorld.Stub.Out.Method(_ => _.Ask(null)).With("Name").Will(Return.Value("Bob")); helloWorld.Stub.Out.Method(_ => _.Ask(null)).With("Age").Will(Return.Value("30")); for (int i = 0; i < ANY_NUMBER; i++) { Verify.That(helloWorld.MockObject.Ask("Name"), Is.EqualTo("Bob"), "Name"); Verify.That(helloWorld.MockObject.Ask("Age"), Is.EqualTo("30"), "Age"); } }
public void MatchesOutputParametersWithSpecialMatcherClass() { Matcher matcher = new ArgumentsMatcher(Is.EqualTo(arg1Value), Is.Out); Invocation invocation = new Invocation( new NamedObject("receiver"), new MethodInfoStub("method", new ParameterInfoStub("in", ParameterAttributes.In), new ParameterInfoStub("out", ParameterAttributes.Out)), new[] { arg1Value, null }); Assert.IsTrue(matcher.Matches(invocation)); }
public void DoesNotMatchAndDoesNotThrowExceptionIfValueSpecifiedForOutputParameter() { Matcher matcher = new ArgumentsMatcher(Is.EqualTo(arg1Value), Is.EqualTo(arg2Value)); Invocation invocation = new Invocation( new NamedObject("receiver"), new MethodInfoStub("method", new ParameterInfoStub("in", ParameterAttributes.In), new ParameterInfoStub("out", ParameterAttributes.Out)), new[] { arg1Value, null }); Assert.IsFalse(matcher.Matches(invocation)); }
public void TestRenderNormal() { // Make sure the renderer draws at least the label's text MockedGraphics.Expects.One.Method( m => m.DrawString(null, RectangleF.Empty, null) ).With(Is.Anything, Is.Anything, Is.EqualTo("Test")); // Let the renderer draw the label into the mocked graphics interface renderer.Render(this.label, MockedGraphics.MockObject); // And verify the expected drawing commands were issues Mockery.VerifyAllExpectationsHaveBeenMet(); }
private void GetPropertyPreviousSetterIndexer(Mock <IParentInterface> mock) { var presenter = new TestPresenter(mock.MockObject); mock.Expects.One.SetProperty(_ => _[4, 5]).To(Is.TypeOf <DateTime>()); mock.Expects.One.GetProperty(_ => _[4, 5]).WillReturnSetterValue(); presenter.SetIndexerPropertyOfMockToInternalValue(); Assert.AreEqual(new DateTime(2011, 5, 1), mock.MockObject[4, 5]); //just checking if it can be done twice mock.Expects.One.GetProperty(_ => _[4, 5]).WillReturnSetterValue(); Assert.AreEqual(new DateTime(2011, 5, 1), mock.MockObject[4, 5]); }
public void TestMethod3() { // // version 2 var persistenceMock = Factory.CreateMock <IPersistence>(); persistenceMock.Expects.One.Method(_ => _.Save(null, null)) .With( Is.Match <string>(key => key == "b"), Is.Match <DateTime>(d => d > DateTime.Now.AddSeconds(-5)) ); persistenceMock.MockObject.Save("b", DateTime.Now); }
private void GetPropertyPreviousSetter2Test(Mock <IParentInterface> mock) { var expectations = new Expectations(2); expectations[0] = mock.Arrange(_ => _.ReadWriteObjectProperty).To(Is.TypeOf <Version>()); expectations[1] = mock.Arrange(_ => _.ReadWriteObjectProperty).WillReturnSetterValue(); var presenter = new TestPresenter(mock.MockObject); presenter.SetPropertyOfMockToInternalValue(); Assert.AreEqual(5, mock.MockObject.ReadWriteObjectProperty.Major); expectations.ForEach(_ => _.Assert()); }
public void ShouldGetArgumentsOfInvokedMethod() { MockFactory mocks = new MockFactory(); string message = "hello"; Mock <IMessageService> serviceMock = mocks.CreateMock <IMessageService>(); MessageController controller = new MessageController(serviceMock.MockObject); serviceMock.Expects.One.Method(_ => _.GetMessage(0, null)).With(Is.EqualTo(23), Is.Anything) .Will(GetArguments.WhenCalled(arguments => (arguments[1] as Action <string>).Invoke(message))); controller.GetMessage(23); Assert.AreEqual(message, controller.Message); }
/// <summary> /// Lets the renderer draw the slider and verifies that the slider used /// the skin elements from the expected state /// </summary> /// <param name="expectedState"> /// Expected state the skin elements should be using /// </param> private void drawAndExpectState(string expectedState) { // Make sure the renderer draws at least two frames MockedGraphics.Expects.One.Method( m => m.DrawElement(null, RectangleF.Empty) ).WithAnyArguments(); MockedGraphics.Expects.One.Method(m => m.DrawElement(null, RectangleF.Empty)).With( Is.StringContaining(expectedState), Is.Anything ); // Let the renderer draw the slider into the mocked graphics interface renderer.Render(this.slider, MockedGraphics.MockObject); // And verify the expected drawing commands were issues Mockery.VerifyAllExpectationsHaveBeenMet(); }
/// <summary> /// Lets the renderer draw the button and verifies that the button used /// the skin elements from the expected state /// </summary> /// <param name="expectedState"> /// Expected state the skin elements should be using /// </param> private void drawAndExpectState(string expectedState) { // Make sure the renderer draws at least a frame and the button's text MockedGraphics.Expects.One.Method(m => m.DrawElement(null, RectangleF.Empty)).With( Is.StringContaining(expectedState), Is.Anything ); MockedGraphics.Expects.One.Method(m => m.DrawString(null, RectangleF.Empty, null)).With( Is.StringContaining(expectedState), Is.Anything, Is.EqualTo("Test") ); // Let the renderer draw the button into the mocked graphics interface renderer.Render(this.button, MockedGraphics.MockObject); // And verify the expected drawing commands were issues Mockery.VerifyAllExpectationsHaveBeenMet(); }
public void TestRenderNormal() { // Make sure the renderer draws at least a frame and the window's title MockedGraphics.Expects.One.Method( m => m.DrawElement(null, RectangleF.Empty) ).WithAnyArguments(); MockedGraphics.Expects.One.Method( m => m.DrawString(null, RectangleF.Empty, null) ).With(Is.Anything, Is.Anything, Is.EqualTo("Title")); // Let the renderer draw the window into the mocked graphics interface renderer.Render(this.window, MockedGraphics.MockObject); // And verify the expected drawing commands were issues Mockery.VerifyAllExpectationsHaveBeenMet(); }
private void WithArgumentsTest(IParentInterface instance) { using (Factory.Ordered()) { Expect.On(instance).One.Method(_ => _.MethodVoid(0, 0, 0)).With(1, 2, 3); Expect.On(instance).One.Method(_ => _.MethodVoid(0, 0, 0)).With(Is.EqualTo(4), Is.AtLeast(5), Is.AtMost(6)); Expect.On(instance).One.Method(_ => _.MethodVoid(0, 0, 0)).With(7, Is.AtLeast(8), 9); Expect.On(instance).One.Method(_ => _.MethodVoid(0, 0, 0)).With( new PredicateMatcher <int>(parameter => parameter == 10), Is.Match <int>(parameter => parameter == 11), Is.Anything); } instance.MethodVoid(1, 2, 3); instance.MethodVoid(4, 5, 6); instance.MethodVoid(7, 8, 9); instance.MethodVoid(10, 11, 12); }
public void SetUp() { factory = new MockFactory(); sensor = factory.CreateMock <AbstractTouchSensor>(); prompt = factory.CreateMock <TextControl>(); prompt.Expects.One.Method(_ => _.show()); prompt.Expects.One.Method(_ => _.hide()); prompt.Expects.One.Method(_ => _.setText("")).With(Is.TypeOf(typeof(String))); sprite = factory.CreateMock <Sprite>(); messageBox = factory.CreateMock <AbstractMessageBox>(); messageBox.Expects.One.Method(_ => _.setMessage("")).With(Is.TypeOf(typeof(String))); messageBox.Expects.One.Method(_ => _.hide()); messageBox.Expects.One.Method(_ => _.show()); gameObject = factory.CreateMock <GameObject>(); gameObject.Expects.Any.Method(_ => _.GetComponent <Sprite>()).WillReturn(sprite.MockObject); }
public void VerifyThatFailsIfMatcherDoesNotMatchValue() { object expected = new NamedObject("expected"); object actual = new NamedObject("actual"); Matcher matcher = Is.EqualTo(expected); try { Verify.That(actual, matcher); } catch (ExpectationException e) { Assert.AreEqual( Environment.NewLine + "Expected: " + matcher + Environment.NewLine + "Actual: <" + actual + ">(NMockTests.NamedObject)", e.Message); return; } Assert.Fail("Verify.That should have failed"); }
public void CanPrependCustomMessageToDescriptionOfFailure() { object expected = new NamedObject("expected"); object actual = new NamedObject("actual"); Matcher matcher = Is.EqualTo(expected); try { Verify.That(actual, matcher, "message {0} {1}", "0", 1); } catch (ExpectationException e) { Assert.AreEqual( "message 0 1" + Environment.NewLine + "Expected: " + matcher + Environment.NewLine + "Actual: <" + actual + ">(NMockTests.NamedObject)", e.Message); return; } Assert.Fail("Verify.That should have failed"); }
private void WithArgumentsTest1(Mock <IParentInterface> mock) { var expectations = new Expectations(4); using (Factory.Ordered()) { expectations[0] = mock.Expects.One.Method(_ => _.MethodVoid(0, 0, 0)).With(1, 2, 3); expectations[1] = mock.Expects.One.Method(_ => _.MethodVoid(0, 0, 0)).With(Is.EqualTo(4), Is.AtLeast(5), Is.AtMost(6)); expectations[2] = mock.Expects.One.Method(_ => _.MethodVoid(0, 0, 0)).With(7, Is.AtLeast(8), 9); expectations[3] = mock.Expects.One.Method(_ => _.MethodVoid(0, 0, 0)).With( new PredicateMatcher <int>(parameter => parameter == 10), Is.Match <int>(parameter => parameter == 11), Is.Anything); } mock.MockObject.MethodVoid(1, 2, 3); mock.MockObject.MethodVoid(4, 5, 6); mock.MockObject.MethodVoid(7, 8, 9); mock.MockObject.MethodVoid(10, 11, 12); expectations.ForEach(_ => _.Assert()); }
public void notTouchedWhenOneThenBothTooLate() { var collection = new SpriteCollection(sprites, null, sensor.MockObject); using (factory.Ordered()) { var heldDown = new[] { TouchPhase.Began, TouchPhase.Moved, TouchPhase.Stationary }; var initialTouch = new[] { TouchPhase.Began }; // first finger down sensor.Expects.One.Method(_ => _.insideSprite(null, null, null)) .With(Is.Anything, Is.EqualTo(sprite1.MockObject), Is.EqualTo(heldDown)) .WillReturn(true); sensor.Expects.One.Method(_ => _.insideSprite(null, null, null)) .With(Is.Anything, Is.EqualTo(sprite1.MockObject), Is.EqualTo(initialTouch)) .WillReturn(true); sensor.Expects.One.Method(_ => _.insideSprite(null, null, null)) .With(Is.Anything, Is.EqualTo(sprite2.MockObject), Is.EqualTo(heldDown)) .WillReturn(false); // second finger down sensor.Expects.One.Method(_ => _.insideSprite(null, null, null)) .With(Is.Anything, Is.EqualTo(sprite1.MockObject), Is.EqualTo(heldDown)) .WillReturn(true); sensor.Expects.One.Method(_ => _.insideSprite(null, null, null)) .With(Is.Anything, Is.EqualTo(sprite1.MockObject), Is.EqualTo(initialTouch)) .WillReturn(false); sensor.Expects.One.Method(_ => _.insideSprite(null, null, null)) .With(Is.Anything, Is.EqualTo(sprite2.MockObject), Is.EqualTo(heldDown)) .WillReturn(true); sensor.Expects.One.Method(_ => _.insideSprite(null, null, null)) .With(Is.Anything, Is.EqualTo(sprite2.MockObject), Is.EqualTo(initialTouch)) .WillReturn(true); } collection.touchedAtSameTime(0f); Assert.That(collection.touchedAtSameTime(0.3f), Iz.False); }
private void OrderedTest1(Mock <IParentInterface> mock) { mock.Expects.One.DelegateBinding(_ => _.Delegate += null); mock.Expects.One.MethodWith(_ => _.Echo("0")).WillReturn("0"); using (Factory.Ordered()) { mock.Expects.One.MethodWith(_ => _.Echo("1")).WillReturn("1"); mock.Expects.One.Method(_ => _.Echo(null)).With(Is.EqualTo("2")).WillReturn("2"); mock.Expects.One.Method(_ => _.Echo(null)).With("3").WillReturn("3"); using (Factory.Unordered()) { mock.Expects.One.MethodWith(_ => _.Echo("4")).WillReturn("4"); mock.Expects.One.Method(_ => _.Echo(null)).With(Is.EqualTo("5")).WillReturn("5"); mock.Expects.One.Method(_ => _.Echo(null)).With("6").WillReturn("6"); } using (Factory.Ordered()) { //do nothing } mock.Expects.One.MethodWith(_ => _.Echo("7")).WillReturn("7"); mock.Expects.One.MethodWith(_ => _.Echo("8")).WillReturn("8"); using (Factory.Unordered()) { mock.Expects.One.MethodWith(_ => _.Echo("9")).WillReturn("9"); mock.Expects.One.MethodWith(_ => _.Echo("10")).WillReturn("10"); using (Factory.Ordered()) { // do nothing } mock.Expects.One.MethodWith(_ => _.Echo("11")).WillReturn("11"); mock.Expects.One.MethodWith(_ => _.Echo("12")).WillReturn("12"); } using (Factory.Unordered()) { //do nothing } mock.Expects.One.MethodWith(_ => _.Echo("13")).WillReturn("13"); } mock.MockObject.Delegate += (s) => { }; mock.MockObject.Echo("0"); mock.MockObject.Echo("1"); mock.MockObject.Echo("2"); mock.MockObject.Echo("3"); mock.MockObject.Echo("6"); mock.MockObject.Echo("4"); mock.MockObject.Echo("5"); mock.MockObject.Echo("7"); mock.MockObject.Echo("8"); mock.MockObject.Echo("12"); mock.MockObject.Echo("11"); mock.MockObject.Echo("10"); mock.MockObject.Echo("9"); mock.MockObject.Echo("13"); }
public void VerifyThatPassesIfMatcherMatchesValue() { object value = new NamedObject("value"); Verify.That(value, Is.EqualTo(value)); }