user3537936
user3537936

Reputation: 3

unit testing viewmodel in silverlight MVVM pattern

I need to do unit testing of view model in Silverlight application with prism. Here the view model that need to test has only one constructor which is taking Dispatcher and EventAggregator as parameters:

public MyViewModel( IDispatcher dispatcher, IEventAggregator aggregator )
        : base(dispatcher, aggregator) {...}

How to mock Dispatcher and EventAggregator parameters in my code in order to create an instance of my view model?

Upvotes: 0

Views: 160

Answers (1)

Martin
Martin

Reputation: 6172

Both ctor arguments are abstracted by interfaces, so for your UnitTest you can write two mocks that implement one interface each and use those...

public class DispatcherMock : IDispatcher { ... }
public class EventAggregatorMock : IEventAggregator { ... }

var sut = new MyViewModel(new DispatcherMock(), new EventAggregatorMock());

...or you can add moq.silverlight (the mock framework I'm using) and let the framework handle the interface details, you don't have to implement it yourself:

var dispatcherMock = new Mock<IDispatcher>();
var aggregatorMock = new Mock<IEventAggregator>();
var sut = new MyViewModel(dispatcherMock.Object, aggregatorMock.Object);

Upvotes: 1

Related Questions