private void MultipleBindingTest(Mock <IParentInterface> mock) { _counter1 = 0; _counter2 = 0; _message = null; EventInvoker invoker = mock.Expects.Exactly(2).EventBinding(_ => _.StandardEvent1 += null); Assert.AreEqual(null, _message); mock.MockObject.StandardEvent1 += ListenerCounter1; invoker.Invoke(); Assert.AreEqual("first", _message); mock.MockObject.StandardEvent1 += ListenerCounter2; invoker.Invoke(); Assert.AreEqual("second", _message); mock.Expects.One.EventBinding(_ => _.StandardEvent1 -= null); mock.MockObject.StandardEvent1 -= ListenerCounter1; invoker.Invoke(); Assert.AreEqual("second", _message); Assert.AreEqual(2, _counter1); Assert.AreEqual(2, _counter2); }
private void MultipleBindingTest(Mock <IParentInterface> mock) { _counter1 = 0; _counter2 = 0; _message = null; EventInvoker <ValueEventArgs> invoker = mock.Expects.Exactly(2).EventBinding <ValueEventArgs>(_ => _.CustomEvent += null); Assert.AreEqual(null, _message); mock.MockObject.CustomEvent += ListenerCounter1; invoker.Invoke(new ValueEventArgs { Value = "first" }); Assert.AreEqual("first", _message); mock.MockObject.CustomEvent += ListenerCounter2; invoker.Invoke(new ValueEventArgs { Value = "second" }); Assert.AreEqual("second", _message); mock.Expects.One.EventBinding(_ => _.CustomEvent -= null); mock.MockObject.CustomEvent -= ListenerCounter1; invoker.Invoke(new ValueEventArgs { Value = "third" }); Assert.AreEqual("third", _message); Assert.AreEqual(2, _counter1); Assert.AreEqual(2, _counter2); }
private void ThrowErrorOnInvokeTest(Mock <IParentInterface> mock) { //Factory.SuppressUnexpectedAndUnmetExpectations(); EventInvoker invoker = mock.Expects.One.EventBinding(_ => _.StandardEvent1 += ThrowListener); mock.MockObject.StandardEvent1 += ThrowListener; try { invoker.Invoke(); Assert.Fail("exception should be thrown."); } catch (Exception err) { #if SILVERLIGHT string message = @"System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.InvalidOperationException: message 3 at NMockTests.MockTests.ExpectsTests.IMethodSyntaxTestsSL.EventBindingTests.ThrowListener(Object sender, EventArgs e)"; #else #if NetFx35 string message = @"System.InvalidOperationException: message 3 at NMockTests.MockTests.ExpectsTests.IMethodSyntaxTests35.EventBindingTests.ThrowListener(Object sender, EventArgs e)"; #else string message = @"System.InvalidOperationException: message 3 at NMockTests.MockTests.ExpectsTests.IMethodSyntaxTests.EventBindingTests.ThrowListener(Object sender, EventArgs e)"; #endif #endif if (!err.ToString().StartsWith(message)) { Assert.Fail("wrong error message. got: " + err); } } }
public void EventInvokingExampleTest() { //To raise or fire the event, call the Invoke method of the MockEventInvoker _initEvent.Invoke(); //Check that the event was fired Assert.AreEqual("Init", _presenter.Status); }
private void RemoveHandlerAndInvokeTest(Mock <IParentInterface> mock) { _message = null; EventInvoker invoker = mock.Expects.One.EventBinding(_ => _.StandardEvent1 -= Listener1); mock.MockObject.StandardEvent1 -= Listener1; invoker.Invoke(); Assert.IsNull(_message); }
private void AddHandlerAndInvokeTest(Mock <IParentInterface> mock) { _message = null; EventInvoker invoker = mock.Expects.One.EventBinding(_ => _.StandardEvent1 += Listener1); mock.MockObject.StandardEvent1 += Listener1; invoker.Invoke(); Assert.AreEqual("message 1", _message); }
private void InvokedHandlerUnbindsTest(Mock <IParentInterface> mock) { EventInvoker <EventArgs> invoker1 = mock.Expects.One.EventBinding <EventArgs>(_ => _.StandardEvent2 += null); EventInvoker <ValueEventArgs> invoker2 = mock.Expects.One.EventBinding <ValueEventArgs>(_ => _.CustomEvent += null); mock.Expects.One.EventBinding(_ => _.StandardEvent2 -= null); mock.Expects.One.EventBinding(_ => _.CustomEvent -= null); mock.MockObject.StandardEvent2 += Unbinder1; mock.MockObject.CustomEvent += Unbinder2; invoker1.Invoke(mock.MockObject, new EventArgs()); invoker2.Invoke(mock.MockObject, new ValueEventArgs()); }
private void RemoveHandlerAndInvokeTest(Mock <IParentInterface> mock) { _message = null; EventInvoker <ValueEventArgs> invoker = mock.Expects.One.EventBinding <ValueEventArgs>(_ => _.CustomEvent -= Listener); mock.MockObject.CustomEvent -= Listener; invoker.Invoke(new ValueEventArgs { Value = "message 2" }); Assert.IsNull(_message); }
private void AddHandlerAndInvokeTest(Mock <IParentInterface> mock) { _message = null; EventInvoker <ValueEventArgs> invoker = mock.Expects.One.EventBinding <ValueEventArgs>(_ => _.CustomEvent += Listener); mock.MockObject.CustomEvent += Listener; invoker.Invoke(new ValueEventArgs { Value = "message 1" }); Assert.AreEqual("message 1", _message); }
private void AddHandlerEmptyDelegateBindingTest(Mock <IParentInterface> mock) { int i = 0; EventInvoker invoker1 = mock.Expects.One.EventBinding(e => e.StandardEvent1 += delegate { }); EventInvoker invoker2 = mock.Expects.One.EventBinding(e => e.StandardEvent2 += delegate { }); mock.MockObject.StandardEvent1 += delegate { i++; }; mock.MockObject.StandardEvent2 += delegate { i++; }; invoker1.Invoke(); invoker2.Invoke(); Assert.AreEqual(2, i); }
private void AddHandlerAnonymousBindingTest(Mock <IParentInterface> mock) { int i = 0; EventInvoker invoker1 = mock.Expects.One.EventBinding(e => e.StandardEvent1 += (s, args) => { }); EventInvoker invoker2 = mock.Expects.One.EventBinding(e => e.StandardEvent2 += (s, args) => { }); mock.MockObject.StandardEvent1 += (s, args) => { i++; }; mock.MockObject.StandardEvent2 += (s, args) => { i++; }; invoker1.Invoke(); invoker2.Invoke(); Assert.AreEqual(2, i); }
private void AddHandlerEmptyDelegateBindingTest(Mock <IParentInterface> mock) { int i = 0; EventInvoker <EventArgs> invoker1 = mock.Expects.One.EventBinding <EventArgs>(e => e.StandardEvent2 += delegate { }); EventInvoker <ValueEventArgs> invoker2 = mock.Expects.One.EventBinding <ValueEventArgs>(e => e.CustomEvent += delegate { }); mock.MockObject.StandardEvent2 += delegate { i++; }; mock.MockObject.CustomEvent += delegate { i++; }; invoker1.Invoke(new EventArgs()); invoker2.Invoke(new ValueEventArgs()); Assert.AreEqual(2, i); }
private void AddHandlerNullBindingTest(Mock <IParentInterface> mock) { _message = null; EventInvoker invoker1 = mock.Expects.One.EventBinding(e => e.StandardEvent1 += null); EventInvoker invoker2 = mock.Expects.One.EventBinding(e => e.StandardEvent2 += null); mock.MockObject.StandardEvent1 += Listener2; mock.MockObject.StandardEvent2 += Listener1; invoker1.Invoke(); Assert.AreEqual("message 2", _message); invoker2.Invoke(); Assert.AreEqual("message 1", _message); }
public void ExceptionFromEventIsTheRightType3() { Mock <IEventProvider> eventProvider = Mocks.CreateMock <IEventProvider>(); Mock <IExceptionThrower> exceptionThrower = Mocks.CreateMock <IExceptionThrower>(); TimeoutException ex = new TimeoutException(); EventInvoker invoker = eventProvider.Expects.One.EventBinding(e => e.Event += null); EventInvoker invoker2 = eventProvider.Expects.One.EventBinding(e => e.Event2 += null); exceptionThrower.Expects.One.MethodWith(_ => _.ThrowException("")).Will(Throw.Exception(ex)); exceptionThrower.Expects.One.MethodWith(_ => _.DoSomethingWith(ex)); Presenter p = new Presenter(eventProvider.MockObject, exceptionThrower.MockObject); Expect.That(() => invoker2.Invoke(null)).Throws <TimeoutException>(); }
private void AddHandlerNullBindingTest(Mock <IParentInterface> mock) { _message = null; EventInvoker <EventArgs> invoker1 = mock.Expects.One.EventBinding <EventArgs>(e => e.StandardEvent2 += null); EventInvoker <ValueEventArgs> invoker2 = mock.Expects.One.EventBinding <ValueEventArgs>(e => e.CustomEvent += null); mock.MockObject.StandardEvent2 += Listener3; mock.MockObject.CustomEvent += Listener; invoker1.Invoke(new EventArgs()); Assert.AreEqual("message 3", _message); invoker2.Invoke(new ValueEventArgs { Value = "message 1" }); Assert.AreEqual("message 1", _message); }
public bool InvokeSetter(InstanceMap instanceMap, T newValue) { eventInvoker?.Invoke(targetObject, new PropertyChangedEventArgs(name)); return(true); }