jeudi 9 avril 2015

Using Moq, how do I mock a method that changes a stream it takes as a parameter?

I'm trying to test the following method:



public void SaveDashboardToPersistentDashboard(
Dashboard dashboard, PersistentDashboard persistentDashboard)
{
using (MemoryStream stream = new MemoryStream())
{
dashboard.SaveToXml(stream);
persistentDashboard.Definition = stream.ToArray();

persistentDashboard.Name = dashboard.Title.Text;
_unitOfWork.CommitChanges();
}
}


The method takes a DevExpress Dashboard, calls SaveToXml (passing in a MemoryStream) and then writes the stream array to a PersistentDashboard POCO's Definition property.


I have no control over the Dashboard class, but would like to mock the SaveToXml method to exclude Dashboard method behaviour from the test. This would involve taking the internal stream and writing an array of known bytes into it.


I'm new to Moq, and can't work out how to change the contents of the stream passed into SaveToXml - at least not without passing the stream into the SaveDashboardToPersistentDashboard method. The stream is incidental to the method's behaviour, so I'm not particularly keen on passing it as a parameter.


Is there any way to achieve what I'm trying to do?


Aucun commentaire:

Enregistrer un commentaire