[Silverlight getting started series] how to unit test unittesting In the event eventaggregator of prism

Source: Internet
Author: User

The previous article explains how to perform unit tests on prism and MEF. Today, let's talk about how to test event publishing and subscription eventaggregator in prism? We all know that eventaggregator is available in Prism:

UsingMicrosoft. Practices. Prism. events;

PrivateIeventaggregator theeventaggregator {Get;Set;}

[Importingconstructor]
PublicMyviewmodel (ieventaggregator eventaggregator)
{
Theeventaggregator=Eventaggregator;
//Todo:
}

You can then publish and subscribe the event ):

Theeventaggregator. getevent<Marketpricesupdatedevent>(). Publish (clonedpricelist );

AboveCodeA Custom Event: marketpriceupdatedevent: (defined as the data type you need. Here is idictionary <string, decimal>)

Public ClassMarketpricesupdatedevent: compositepresentationevent<Idictionary<String,Decimal>
{
}

If the last line of a function is to publish an event without returning a value, how can we perform unit testing on it? How can I test whether this event has been publish?

We can write a mock eventaggregator:

 Class  Mockeventaggregator: ieventaggregator
{
Dictionary < Type, Object > Events = New Dictionary < Type, Object > ();
Public Teventtype getevent < Teventtype > () Where Teventtype: eventbase, New ()
{
Return (Teventtype) events [ Typeof (Teventtype)];
}

Public Void Addmapping < Teventtype > (Teventtype mockevent)
{
Events. Add ( Typeof (Teventtype), mockevent );
}
}

Class Mockpriceupdatedeventaggregator: mockeventaggregator
{
Public Mockmarketpricesupdatedevent mockmarketpriceupdatedevent = New Mockmarketpricesupdatedevent ();
Public Mockpriceupdatedeventaggregator ()
{
// Note that this is not mock, it is the original one!
Addmapping < Marketpricesupdatedevent > (Mockmarketpriceupdatedevent );
}

Public Class Mockmarketpricesupdatedevent: marketpricesupdatedevent
{
Public Bool Publishcalled;
Public Idictionary < String , Decimal > Publishargumentpayload;
Public Eventhandler publishcalledevent;

Private Void Onpublishcalledevent ( Object Sender, eventargs ARGs)
{
If (Publishcalledevent ! = Null )
Publishcalledevent (sender, argS );
}

Public Override Void Publish (idictionary < String , Decimal > Payload)
{
Publishcalled = True ;
Publishargumentpayload = Payload;
Onpublishcalledevent ( This , Eventargs. Empty );
}
}
}

Then the mock event can be used in the unit test class:

[Testmethod]
Public Void Testpublishedevent ()
{
VaR eventagregator = New Mockpriceupdatedeventaggregator ();
VaR marketfeed = New Testablemarketfeedservice (eventagregator );
Assert. istrue (marketfeed. symbolexists ( " Stock0 " ));

Marketfeed. invokeupdateprices ();

// Verify event Publishing
Assert. istrue (eventagregator. mockmarketpriceupdatedevent. publishcalled );

// Validate event parameters
VaR Payload = Eventagregator. mockmarketpriceupdatedevent. publishargumentpayload;
Assert. isnotnull (payload );
Assert. istrue (payload. containskey ( " Stock0 " ));
Assert. areequal (marketfeed. getprice ( " Stock0 " ), Payload [ " Stock0 " ]);
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.