我写了服务层代码如下:
public GetAllCategoriesResponse GetAllCategories()
{
GetAllCategoriesResponse response = new GetAllCategoriesResponse();
IEnumerable<Category> categories = _categoryRepository.GetAll();
response.Categories = categories.ConvertToCategoryViews();
return response;
}
然后我按如下方式编写了我的Test类:
[TestClass]
public class ProductCatalogServiceTest
{
Mock<ICategoryRepository> _categoryRepository;
ProductCatalogService _catalogService;
[TestInitialize]
public void Setup()
{
_categoryRepository = new Mock<ICategoryRepository>();
_catagoryService = new ProductCatagoryService( _categoryRepository.Object);
}
[TestMethod]
public void Test_All_Catagories()
{
AutoMapperBootStrapper.ConfigureAutoMapper();
var catagories = new List<Category>();
catagories.Add(new Category() {Id=1, Name = "Half Shirt" });
catagories.Add(new Category() {Id=2, Name = "Full Shirt" });
catagories.Add(new Category() {Id=4, Name = "Pant" });
_categoryRepository.Setup(m => m.GetAll()).Returns(catagories.AsEnumerable());
var result = _catagoryService.GetAllCategories();
var response = new GetAllCategoriesResponse();
response.Categories = catagories.ConvertToCategoryViews();
Assert.AreEqual(response, result);
}
}
Category和CategoryView之间的映射如下:
public class AutoMapperBootStrapper
{
public static void ConfigureAutoMapper()
{
// Category
Mapper.CreateMap<Category, CategoryView>();
}
}
当我运行我的测试时,会显示以下消息: Assert.AreEqual失败。预期:Shoppingcart.Services.Messages.GetAllCategoriesResponse&gt;。实际:Shoppingcart.Services.Messages.GetAllCategoriesResponse&GT;