【发布时间】:2012-10-06 15:05:18
【问题描述】:
它已经对其进行了研究,并发现了几个有趣的链接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);
}
【问题讨论】:
-
查看测试方法->> //这里有问题!! (调用GetFiles,如何mock结果??)
标签: c# unit-testing tdd moq mstest