I have a class that implements INotifyPropertyChanged and I need to test if this interface is implemented correctly. I want to do this using a Rhino Mock object.
class MyClass : INotifyPropertyChanged
{
public int X
{
get => ...;
set => ... // should check if value changes and raise event PropertyChanged
}
}
What I want to test, is that when X changes value, that event PropertyChanged is called exactly once, with the proper parameters.
MyClass testObject = new MyClass();
// the mock:
PropertyChangedEventHandler a = MockRepository.GenerateMock<PropertyChangedEventHandler>();
testObject.PropertyChanged += a;
// expect that the mock will be called exactly once, with the proper parameters
a.Expect( (x) => ???)
.Repeat()
.Once();
// change X, and verify that the event handler has been called exactly once
testObject.X = testObject.X + 1;
a.VerifyAllExpectations(); ???
I think I'm on the right path, but I can't get it working.
Sometimes, there really is no need to use a mock if there are not knock-on effects of using the real thing.
The following simple example creates an instance of the delegate and verifies the expected behavior
What I want to test, is that when X changes value, that event PropertyChanged is called exactly once, with the proper parameters.
[TestClass]
public class MyClassTests {
[TestMethod]
public void Should_Call_PropertyChanged_Once() {
//Arrange
//Store calls
IDictionary<string, int> properties = new Dictionary<string, int>();
PropertyChangedEventHandler handler = new PropertyChangedEventHandler((s, e) => {
if (!properties.ContainsKey(e.PropertyName))
properties.Add(e.PropertyName, 0);
properties[e.PropertyName]++;
});
MyClass testObject = new MyClass();
testObject.PropertyChanged += handler;
string expectedPropertyName = nameof(MyClass.X);
int expectedCount = 1;
//Act
testObject.X = testObject.X + 1;
//Assert - using FluentAssertions
properties.Should().ContainKey(expectedPropertyName);
properties[expectedPropertyName].Should().Be(expectedCount);
}
class MyClass : INotifyPropertyChanged {
public event PropertyChangedEventHandler PropertyChanged = delegate { };
void raisePropertyChanged([CallerMemberName]string propertyName = null) {
PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
int x;
public int X {
get => x;
set {
if (value != x) {
x = value;
raisePropertyChanged();
}
}
}
}
}
Related
I am using WPF and MVVM pattern, but that's not really relevant in this case. In my ViewModel MainVM, I have a property like this:
public int Posts {
get { return this.posts; }
set {
this.posts = value;
this.OnPropertyChanged(nameof(this.Posts));
}
}
Through properties, I supply MainVM a list of Foo. (Actually, I want to supply it using the constructor, but I can't. Read below) And there is a start method on the MainVM which does as follows:
foreach(Foo foo in this.Foos){ // this.Foos is just a List<Foo>
Task.Run(() => foo.Bar());
}
In the execution of Foo.Bar (There are lots of methods being called in Bar, so passing an Action to bar itself is not really feasible), it is supposed to update a value, and that value is MainVM.Posts. Passing MainVM to Foo is not an option. So I thought of passing an Action instead. So I wrote this Method in MainVM.
public void IncrementPosts()
{
lock(this.whatever)
{
this.Posts++;
}
}
Then I made Foo take an Action in the constructor. So here's how the Foos are created and passed to MainVM.
var vm = new MainVM();
var foo1 = new Foo(vm.IncrementPosts);
var foo2 = new Foo(vm.IncrementPosts);
vm.Foos = new List<Foo>() {foo1, foo2};
Whilst this works, I feel that it looks ugly. First of all, MainVM needs a list of Foo to work properly. So it's supposed to be supplied to the constructor, right? But as a Foo needs an action which points to a method in MainVM, I can't do that. And all of this looks like a hack.
Is there a better way of doing this?
If I understand, what are you want to do..
void Main()
{
var vm = new MainVM(new FooFactory());
Console.WriteLine(vm.Posts);
vm.ExecuteFoo();
Console.WriteLine(vm.Posts);
}
public class Foo
{
private Action _action;
public Foo(Action action)
{
_action = action;
}
public void RunAction()
{
_action();
}
}
public class FooFactory
{
public Foo[] CreateFoo(MainVM vm)
{
return new[] { new Foo(()=>vm.Posts++) };
}
}
public class MainVM : INotifyPropertyChanged
{
public MainVM(FooFactory factory)
{
_foos = factory.CreateFoo(this);
}
public void ExecuteFoo()
{
foreach(var foo in _foos)
foo.RunAction();
}
private Foo[] _foos;
private int posts;
public int Posts
{
get { return this.posts; }
set
{
this.posts = value;
this.OnPropertyChanged();
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged([CallerMemberName]string propertyName = null)
{
var local = PropertyChanged;
if (local != null)
{
local(this, new PropertyChangedEventArgs(propertyName));
}
}
}
The following code should be self explanetory: we have an adaptor, who consumes events from the transport (layer), which holds the MessageRegistrar (object type because we can't tell it's type, and basically because this is legacy code :-) ). The transport layer have a concrete which have an event.
I want to test a case where the event is triggered, so..
After hours of trying to figure why it won't pass, I present the following challenge:
[TestFixture]
public class AdaptorTests
{
public delegate void TracksEventHandler(object sender, List<int> trklst);
public class MyEventHolder
{
public virtual event TracksEventHandler EventName;
}
public interface ITransport
{
object MessageRegistrar { get; }
}
public class MyTransport : ITransport
{
private readonly MyEventHolder m_eventHolder;
public MyTransport(MyEventHolder eventHolder)
{
m_eventHolder = eventHolder;
}
public virtual object MessageRegistrar
{
get { return m_eventHolder; }
}
}
public class MyAdaptor
{
private readonly ITransport m_transport;
public MyAdaptor(ITransport transport)
{
EventTriggered = false;
m_transport = transport;
}
public void Connect()
{
MyEventHolder eventHolder = m_transport.MessageRegistrar as MyEventHolder;
if (eventHolder != null)
eventHolder.EventName += EventHolderOnEventName;
}
private void EventHolderOnEventName(object sender, List<int> trklst)
{
EventTriggered = true;
}
public bool EventTriggered { get; private set; }
}
[Test]
public void test1()
{
Mock<MyEventHolder> eventHolderMock = new Mock<MyEventHolder> {CallBase = true};
Mock<MyTransport> transportMock = new Mock<MyTransport>(eventHolderMock.Object) {CallBase = true};
MyAdaptor adaptor = new MyAdaptor(transportMock.Object);
adaptor.Connect();
MyEventHolder eventHolder = transportMock.Object.MessageRegistrar as MyEventHolder;
Mock.Get(eventHolder).Raise(eh => eh.EventName += null, new List<int>());
Assert.IsTrue(adaptor.EventTriggered);
}
[Test]
public void test2()
{
Mock<MyEventHolder> eventHolderMock = new Mock<MyEventHolder> { CallBase = true };
Mock<MyTransport> transportMock = new Mock<MyTransport>(eventHolderMock.Object) { CallBase = true };
MyAdaptor adaptor = new MyAdaptor(transportMock.Object);
adaptor.Connect();
MyEventHolder eventHolder = transportMock.Object.MessageRegistrar as MyEventHolder;
Mock.Get(eventHolder).Raise(eh => eh.EventName += null, null, new List<int>());
Assert.IsTrue(adaptor.EventTriggered);
}
}
My question is: why wont the test (at least one of them) pass?
EDIT #151217-0822 Addded 'adaptor.Connect()' to the original post (still won't fix the issue).
WORKAROUND
Credits to #Patrick Quirk: Thanks!!
For those encountering the same issue: after I understood what Patrick-Quirk detected, and trying couple of failed workarounds, I've ended up adding the following verified fix: 'eventHolder.FireEventNameForTestings(new List());':
public class MyEventHolder
{
public virtual event TracksEventHandler EventName;
public virtual void FireEventNameForTestings(List<int> trklst)
{
TracksEventHandler handler = EventName;
if (handler != null)
handler(this, trklst);
}
}
[Test]
public void test3()
{
Mock<MyEventHolder> eventHolderMock = new Mock<MyEventHolder> { CallBase = true };
Mock<MyTransport> transportMock = new Mock<MyTransport>(eventHolderMock.Object) { CallBase = true };
MyAdaptor adaptor = new MyAdaptor(transportMock.Object);
adaptor.Connect();
MyEventHolder eventHolder = transportMock.Object.MessageRegistrar as MyEventHolder;
eventHolder.FireEventNameForTestings(new List<int>());
Assert.IsTrue(adaptor.EventTriggered);
}
HTH..
It seems that CallBase and Raise() have an unexpected (to me) interaction.
When you are attaching an event handler to a virtual event on a mock, you go through this code in Moq:
if (invocation.Method.IsEventAttach())
{
var delegateInstance = (Delegate)invocation.Arguments[0];
// TODO: validate we can get the event?
var eventInfo = this.GetEventFromName(invocation.Method.Name.Substring(4));
if (ctx.Mock.CallBase && !eventInfo.DeclaringType.IsInterface)
{
invocation.InvokeBase();
}
else if (delegateInstance != null)
{
ctx.AddEventHandler(eventInfo, (Delegate)invocation.Arguments[0]);
}
return InterceptionAction.Stop;
}
You can see that if CallBase is true, then it will add your handler to the concrete object's event (via invocation.InvokeBase()). If CallBase is false, it will add it to an invocation list on the mock (via AddEventHandler). Now let's look at the code for Raise(), which gets the event object from the Expression and then calls DoRaise():
internal void DoRaise(EventInfo ev, EventArgs args)
{
// ... parameter validation
foreach (var del in this.Interceptor.InterceptionContext.GetInvocationList(ev).ToArray())
{
del.InvokePreserveStack(this.Object, args);
}
}
See the call to GetInvocationList()? That retrieves the invocation list from the mock that I mentioned above. This code never invokes the actual event on the base object.
So, it seems there's no way to raise an event on a mocked object where CallBase is set to true.
The only workaround I see, if you require CallBase being true, is to add a method to your concrete MyEventHolder to trigger your event. Obviously what you posted is a simplified example so I can't give you more guidance than that, but hopefully I've shown you why what you have does not work.
I use a library[1] that defines some stuff I would like to make use of:
public delegate void FooHandler(int i);
public delegate void BarHandler(string s);
public class Foo
{
public event FooHandler Fooh;
}
public class Bar
{
public event BarHandler Barh;
}
I would like to have a short term memory, that I can "attach" to these events:
public class ShortTermMemory<T>
{
public ShortTermMemory(??? arg)
{
arg += t => Remeber = t;
}
public T Remember { get; private set; }
}
and attach them somewhere like
var foo = new Foo();
var bar = new Bar();
var intmemory = new ShortTermMemory<int>(foo.Fooh);
var stringmemory = new ShortTermMemory<string>(bar.Barh);
This is impossible because:
??? is (somewhat understandably) not a valid type declaration, and
"The event can only be used on the left hand side of += or -="
Is there anything I can do to fix this, or is this fundamentally impossible in C#? I would like to have compile time guarantees that ShortTermMemory is only "fed" by one event source, which is known at compile time to have a single argument of type T.
[1]: Example implementation for demonstration purposes only
With the compile time guarantees you are expecting it isn't possible in C#. There are a few problems there:
events are backed by a private delegate field which has a combined invocation list. This is considered an implementation detail, thus the private access.
We need to reference the event's delegate field to apply add and remove (+=/-=) operators with Delegate.Combine/Remove.
As C# handles delegates as own types, we should have a generic definition of both FooHandler and BarHandler.
You're missing an unsubscription from the event, possibly placed in the destructor of ShortTermMemory`1. At this time, the event object may not exist anymore, see How to save a ref variable for later use?
Working solution:
public delegate void Handler<T> ( T i );
public class Foo
{
public event Handler<int> Fooh;
public void Set(int i) {
if (Fooh != null) {
Fooh(i);
}
}
}
public class ShortTermMemory<T>
{
object obj = null;
string eventName;
Handler<T> handler;
public ShortTermMemory (object obj, string eventName) {
this.obj = obj;
this.eventName = eventName;
this.handler = new Handler<T>(Set);
Type type = obj.GetType();
EventInfo info = type.GetEvent(eventName);
info.AddEventHandler(obj, handler);
}
~ShortTermMemory() {
if (obj != null) {
Type type = obj.GetType();
EventInfo info = type.GetEvent(eventName);
info.RemoveEventHandler(obj, handler);
}
}
public T Remember { get; set; }
public void Set(T t) {
Remember = t;
}
}
var foo = new Foo();
var intmemory1 = new ShortTermMemory<int>(foo, "Fooh");
var intmemory2 = new ShortTermMemory<int>(foo, "Fooh");
foo.Set(10);
The Reactive Extensions allow you to easily subscribe to an event using Observable.FromEventPattern, but I can't find anything on how you might implement an event when you have an IObservable.
My situation is this: I need to implement an interface which contains an event. That event is supposed to be called whenever a certain value of my object changes, and for thread safety reasons I need to call this event on a certain SynchronizationContext. I am also supposed to call each event handler with the current value on registration.
public interface IFooWatcher
{
event FooChangedHandler FooChanged;
}
Getting an observable that does what I want is rather easy with Rx using BehaviorSubject:
public class FooWatcher
{
private readonly BehaviorSubject<Foo> m_subject;
private readonly IObservable<Foo> m_observable;
public FooWatcher(SynchronizationContext synchronizationContext, Foo initialValue)
{
m_subject = new BehaviorSubject<Foo>(initialValue);
m_observable = m_subject
.DistinctUntilChanged()
.ObserveOn(synchronizationContext);
}
public event FooChangedHandler FooChanged
{
add { /* ??? */ }
remove { /* ??? */ }
}
}
Now I am looking for an easy way to have the add and remove functions subscribe and unsubscribe the passed FooChangedHandler as an Observer<Foo> on m_observable. My current implementation looks similar to this:
add
{
lock (m_lock)
{
IDisposable disp = m_observable.Subscribe(value);
m_registeredObservers.Add(
new KeyValuePair<FooChangedHandler, IDisposable>(
value, disp));
}
}
remove
{
lock (m_lock)
{
KeyValuePair<FooChangedHandler, IDisposable> observerDisposable =
m_registeredObservers
.First(pair => object.Equals(pair.Key, value));
m_registeredObservers.Remove(observerDisposable);
observerDisposable.Value.Dispose();
}
}
However, I hope to find an easier solution, because I need to implement several of these events (of differing handler types). I tried to roll my own generic solution but it creates some additional problems that need to be worked around (in particular, how you generically work with a delegate that takes a parameter of T), so I would prefer to find an existing solution that bridges the gap in this direction - just as FromEventPattern does the reverse.
You could do this:
public event FooChangedHandler FooChanged
{
add { m_observable.ToEvent().OnNext += value; }
remove { m_observable.ToEvent().OnNext -= value; }
}
However, on the remove, I think perhaps you just may want to dispose of the subscription ... or perhaps get the Action from ToEvent() and store that as a member. Untested.
EDIT: You'll have to use Action instead of a FooChangedHandler delegate, however.
EDIT 2: Here's a tested version. I suppose you need to use FooChangedHandler, however, since you have a bunch of these pre-existing handlers?
void Main()
{
IObservable<Foo> foos = new [] { new Foo { X = 1 }, new Foo { X = 2 } }.ToObservable();
var watcher = new FooWatcher(SynchronizationContext.Current, new Foo { X = 12 });
watcher.FooChanged += o => o.X.Dump();
foos.Subscribe(watcher.Subject.OnNext);
}
// Define other methods and classes here
//public delegate void FooChangedHandler(Foo foo);
public interface IFooWatcher
{
event Action<Foo> FooChanged;
}
public class Foo {
public int X { get; set; }
}
public class FooWatcher
{
private readonly BehaviorSubject<Foo> m_subject;
public BehaviorSubject<Foo> Subject { get { return m_subject; } }
private readonly IObservable<Foo> m_observable;
public FooWatcher(SynchronizationContext synchronizationContext, Foo initialValue)
{
m_subject = new BehaviorSubject<Foo>(initialValue);
m_observable = m_subject
.DistinctUntilChanged();
}
public event Action<Foo> FooChanged
{
add { m_observable.ToEvent().OnNext += value; }
remove { m_observable.ToEvent().OnNext -= value; }
}
}
Given that you are already mixing the boundaries between reactive and more normal code, you could do a less reactive version. To start simply declare a normal event pattern
public event FooChangedHandler FooChanged;
protected void OnFooChanged(Foo)
{
var temp = FooChanged;
if (temp != null)
{
temp(new FooChangedEventArgs(Foo));
}
}
and then simply connect the observable to it in the constructor
m_Observable.Subscribe(foo => OnFooChanged(foo));
It's not very Rx but it is incredibly simple.
I'm developing an asp.net (classic) application trying to implement the MVP pattern using this example. In trying to unit test my presenter and using the following pattern, the psuedocode for which looks like so
//base view interface
public interface IView
{
event EventHandler Init;
event EventHandler Load;
bool IsPostBack { get; }
void DataBind();
bool IsValid { get;}
}
//presenter psuedo code
public class SomePresenter
{
public SomePresenter(ISomeDomainService service, IView someView)
{
...
//HOW DO WE TEST/VERIFY THAT THIS REGISTRATION OCCURS?
someView.Init += OnInit;
someView.Load += OnLoad;
}
}
...
//consuming code that exercises the above code, that needs to be tested
var presenter = new SomePresenter(someDomainService, someView);
How do I verify that the presenter is doing what is expected i.e. registering for the Init and Load events? While this is easily done in the Phil Haack's example using Rhino mocks...
[Test]
public void VerifyAttachesToViewEvents()
{
viewMock.Load += null;
LastCall.IgnoreArguments();
viewMock.PostSaved += null;
LastCall.IgnoreArguments();
mocks.ReplayAll();
new PostEditController(viewMock,
this.dataServiceMock);
mocks.VerifyAll();
}
... how can we do this using MOQ?
The moq 4.13 introduced this feature. Now it is possible to verify if add\remove has been invoked. Therefore four new methods have been introduced:
SetupAdd
SetupRemove
VerifyAdd
VerifyRemove
Example
var mock = new Mock<IAdder<EventArgs>>();
mock.SetupAdd(m => m.Added += (sender, args) => { });
mock.Object.Added += (sender, args) => { };
mock.Object.Added += (sender, args) => { };
mock.VerifyAdd(m => m.Added += It.IsAny<EventHandler>(), Times.Exactly(2));
NB: Notice that in order to verify at least one setup should be added. The reason is to keep backward compatibility with the older version of moq.
It would appear that this functionality is not currently available in moq, but may appear in a future version (I had a look in the 4.0.812.4 beta, but it doesn't seem to be there).
It may be worth asking the question, "why does SomePresenter need to subscribe to the View's Load and Init events?" Presumably it is because the SomePresenter class needs to respond to those events. So it might be better to use the Raise method on your Mock<IView> to raise the Load and Init events, and then assert that SomePresenter did the right thing in response to them.
I know it's maybe too late for #Dilip, but this answer can be helpful for those who are trying to do the same.
Here is the test class
public delegate void SubscriptionHandler<T>(string name, T handler);
public class SomePresenterTest
{
[Test]
public void Subscription_Test()
{
var someServiceMock = new Mock<ISomeDomainService>();
var viewMock = new Mock<IView>();
//Setup your viewMock here
var someView = new FakeView(viewMock.Object);
EventHandler initHandler = null;
someView.Subscription += (n, h) => { if ((nameof(someView.Init)).Equals(n)) initHandler=h; };
Assert.IsNull(initHandler);
var presenter = new SomePresenter(someServiceMock.Object, someView);
Assert.IsNotNull(initHandler);
Assert.AreEqual("OnInit", initHandler.Method?.Name);
}
}
FakeView is a decorator implemented as follow (pay attention to Events:Init/Load{add;remove}):
public class FakeView : IView
{
public event SubscriptionHandler<EventHandler> Subscription;
public event SubscriptionHandler<EventHandler> Unsubscription;
private IView _view;
public FakeView(IView view)
{
Assert.IsNotNull(view);
_view = view;
}
public bool IsPostBack => _view.IsPostBack;
public bool IsValid => _view.IsValid;
public event EventHandler Init
{
add
{
Subscription?.Invoke(nameof(Init), value);
_view.Init += value;
}
remove
{
Unsubscription?.Invoke(nameof(Init), value);
_view.Init -= value;
}
}
public event EventHandler Load
{
add
{
Subscription?.Invoke(nameof(Load), value);
_view.Init += value;
}
remove
{
Unsubscription?.Invoke(nameof(Load), value);
_view.Init -= value;
}
}
public void DataBind()
{
_view.DataBind();
}
}
I spent some time with this question and the solution which I'm using in my project is:
Unit test:
// Arrange
TestedObject.Setup(x => x.OnEvent1());
TestedObject.Setup(x => x.OnEvent2());
// Act
TestedObject.Object.SubscribeEvents();
TestedObject.Raise(x => x.Event1 += null);
TestedObject.Raise(x => x.Event2 += null);
// Assert
TestedObject.Verify(x => x.OnEvent1(), Times.Once());
TestedObject.Verify(x => x.OnEvent2(), Times.Once());
Tested method:
this.Event1 += OnEvent1;
this.Event2 += OnEvent2;
So, first you have to mock the methods which you will assign the events, after you call the method which you want to test, and finally raise all subscribed events. If the event is really subscribed, you can check with Moq if the assigned method is called.
GLHF!