[unit testing newbie] [C#]metodo di derisione con azione <T> parametro
Si consideri il seguente scenario:
sto utilizzando Silverlight e chiamare un servizio WCF. Silverlight può solo chiamare servizi WCF in modo asincrono. Costruisco un wrapper attorno al servizio WCF in modo che possa lavorare con i parametri Action. (rende il codice cliente molto più pulito).
Quindi ho un servizio asincrono che recupera le sale riunioni.
public interface IMeetingRoomService
{
void GetRooms(Action<List<MeetingRoom>> result);
}
tornitura GetRooms in List<MeetingRoom> GetRooms()
non è un'opzione.
Desidero utilizzare questo servizio in un ViewModel per impostare una proprietà pubblica denominata Camere.
public class SomeViewModel
{
private readonly IMeetingRoomService _meetingRoomService;
public List<MeetingRoom> Rooms { get; set; }
public SomeViewModel(IMeetingRoomService meetingRoomService)
{
this._meetingRoomService = meetingRoomService;
}
public void GetRooms()
{
// Code that calls the service and sets this.Rooms
_meetingRoomService.GetRooms(result => Rooms = result);
}
}
Voglio testare l'implementazione di SomeViewModel.GetRooms(). (Per questa domanda ho scritto rapidamente l'implementazione ma in realtà sto cercando di usare TDD.)
Come posso finire questo test? Sto usando NUnit e Moq.
[Test]
public void GetRooms_ShouldSetRooms()
{
var theRooms = new List<MeetingRoom>
{
new MeetingRoom(1, "some room"),
new MeetingRoom(2, "some other room"),
};
var meetingRoomService = new Mock<IMeetingRoomService>();
//How do I setup meetingRoomService so that it gives theRooms in the Action??
var viewModel = new SomeViewModel(meetingRoomService.Object);
viewModel.GetRooms();
Assert.AreEqual(theRooms, viewModel .Rooms);
}
EDIT:
Soluzione
risposta Leggi Stephane di prima.
Questo è il codice di prova ho finito per scrivere grazie alla risposta di Stéphane:
[Test]
public void GetRooms_ShouldSetRooms()
{
var meetingRoomService = new Mock<IMeetingRoomService>();
var shell = new ShellViewModel(meetingRoomService.Object);
var theRooms = new List<MeetingRoom>
{
new MeetingRoom(1, "some room"),
new MeetingRoom(2, "some other room"),
};
meetingRoomService
.Setup(service => service.GetRooms(It.IsAny<Action<List<MeetingRoom>>>()))
.Callback((Action<List<MeetingRoom>> action) => action(theRooms));
shell.GetRooms();
Assert.AreEqual(theRooms, shell.Rooms);
}
mi piacerebbe tornare un '' task –
CodesInChaos
ehhh ..... cosa? –