它已經研究過它,並發現了幾個有趣的鏈接like this。 但對我的問題,他們沒有幫助我。模擬擴展方法作爲接口
代碼
我有以下接口
public interface IViewFolderReference
{
string FolderName { get; set; }
}
擴展
public static ICollection<TFile> GetFiles<TFile>(this IViewFolderReference view)
where TFile: class, IFile
{
var folder = view.GetFolder();
return folder.Exists ?
Mapper.Map<ICollection<TFile>>(folder.GetFiles())
: null;
}
水泥混凝土類
public class ProcessoViewModel : IViewFolderReference
{
public string FolderName { get; set; }
public ICollection<File> Arquivos { get; set; }
...
}
試驗方法
[TestMethod]
public void Ao_salvar_processo_adicionar_dois_itens()
{
// Arrange
var vm = new Mock<ProcessoViewModel>();
vm.Object.Arquivos = new List<File>() {
new File { FileName = "Arquivo 1.jpg", DisplayName = "Arquivo 1" }
,new File { FileName = "Arquivo 2.doc", DisplayName = "Arquivo 2" }
};
//Act
controller.salvar(vm.Object); // Problem here!! (GetFiles is called, How can mock the result??)
//Assert
var processoDb = repositorio.Query<Processo>().SingleOrDefault(p => p.Imovel == vm.Object.Imovel && vm.Object.DataEntrada == p.DataEntrada);
Assert.IsNotNull(processoDb.Arquivos);
Assert.AreEqual(processoDb.Arquivos.Count, 2);
}
你的問題是什麼? – Sklivvz
查看測試方法 - >> //問題在這裏! (GetFiles被調用,如何模擬結果??) – ridermansb