public void TrackMockBehaviors() { var stunt = new FakeStunt(); // Forces initialization of the default mock. Assert.NotNull(stunt.Mock); var setup = new MockSetup( new MethodInvocation(stunt, typeof(FakeStunt).GetMethod("Do")), Array.Empty <IArgumentMatcher>()); var behavior = new MockBehaviorPipeline(setup); stunt.AddBehavior(behavior); stunt.AddBehavior(StuntBehavior.Create((m, n) => n().Invoke(m, n))); Assert.Equal(3, stunt.Behaviors.Count); Assert.Single(stunt.Mock.Setups); Assert.Same(behavior, stunt.Mock.GetPipeline(setup)); stunt.Behaviors.Remove(behavior); Assert.Equal(2, stunt.Behaviors.Count); Assert.Empty(stunt.Mock.Setups); }
public void AddsHandler() { var mock = new EventfulMock(); mock.Behaviors.Add(new EventBehavior()); mock.Behaviors.Add(StuntBehavior.Create((m, n) => m.CreateValueReturn(null))); EventHandler handler = (_, __) => { }; mock.Empty += handler; Assert.True(mock.AsMock().State.TryGetValue <Delegate>(nameof(IEventful.Empty), out var e)); Assert.Contains(handler, e.GetInvocationList()); }
public void RaisesEventHandlerIfRaiserInContextOnAdd() { var mock = new EventfulMock(); mock.Behaviors.Add(new EventBehavior()); mock.Behaviors.Add(StuntBehavior.Create((m, n) => m.CreateValueReturn(null))); var called = false; EventHandler handler = (_, __) => called = true; mock.Empty += handler; CallContext <EventRaiser> .SetData(EventRaiser.Empty); mock.Empty += null; Assert.True(called); }
public void WhenInvokingPipeline_ThenSkipsNonApplicableBehaviors() { var firstCalled = false; var secondCalled = false; var targetCalled = false; var pipeline = new BehaviorPipeline( StuntBehavior.Create((m, n) => { firstCalled = true; return(n().Invoke(m, n)); }), StuntBehavior.Create((m, n) => { secondCalled = true; return(n().Invoke(m, n)); }, m => false)); Action a = WhenInvokingPipeline_ThenInvokesAllBehaviorsAndTarget; pipeline.Invoke(new MethodInvocation(this, a.GetMethodInfo()), new ExecuteDelegate((m, n) => { targetCalled = true; return(m.CreateValueReturn(null)); })); Assert.True(firstCalled); Assert.False(secondCalled); Assert.True(targetCalled); }
public void RaisesEventArgsIfRaiserInContextOnAdd() { var mock = new EventfulMock(); mock.Behaviors.Add(new EventBehavior()); mock.Behaviors.Add(StuntBehavior.Create((m, n) => m.CreateValueReturn(null))); var expected = new Args(); var actual = default(Args); EventHandler <Args> handler = (sender, args) => actual = args; mock.WithArgs += handler; CallContext <EventRaiser> .SetData(new EventArgsEventRaiser(expected)); mock.WithArgs += null; Assert.Same(expected, actual); }
public void RaisesActionIfRaiserInContextOnAdd() { var mock = new EventfulMock(); mock.Behaviors.Add(new EventBehavior()); mock.Behaviors.Add(StuntBehavior.Create((m, n) => m.CreateValueReturn(null))); var expected = 5; var actual = 0; Action <int> handler = i => actual = i; mock.Action += handler; CallContext <EventRaiser> .SetData(new CustomEventRaiser(new object[] { expected })); mock.Action += null; Assert.Equal(expected, actual); }
public void RaisesPropertyChangedIfRaiserInContextOnAdd() { var mock = new EventfulMock(); mock.Behaviors.Add(new EventBehavior()); mock.Behaviors.Add(StuntBehavior.Create((m, n) => m.CreateValueReturn(null))); var expected = new PropertyChangedEventArgs("Foo"); var actual = default(PropertyChangedEventArgs); PropertyChangedEventHandler handler = (sender, args) => actual = args; mock.PropertyChanged += handler; CallContext <EventRaiser> .SetData(new EventArgsEventRaiser(expected)); mock.PropertyChanged += null; Assert.Same(expected, actual); }
public void RaisesCustomDelegateIfRaiserInContextOnAdd() { var mock = new EventfulMock(); mock.Behaviors.Add(new EventBehavior()); mock.Behaviors.Add(StuntBehavior.Create((m, n) => m.CreateValueReturn(null))); var(id, name) = (5, "foo"); var(id2, name2) = (0, ""); CustomDelegate handler = (i, n) => { id2 = i; name2 = n; }; mock.Custom += handler; CallContext <EventRaiser> .SetData(new CustomEventRaiser(new object[] { id, name })); mock.Custom += null; Assert.Equal(id, id2); Assert.Equal(name, name2); }