我正在尝试嘲笑UnitOfWork但是我需要一些帮助,因为我无法正确地实现我的结果
所以我有以下BooksController: -
attempt to call a nil value (global 'loadstring')
以下BooksRepository: -
public class BooksController : BaseController
{
private IUnitOfWork UnitOfWork;
public BooksController()
{
UnitOfWork = new UnitOfWork();
}
public BooksController(IUnitOfWork unitOfWork)
{
UnitOfWork = unitOfWork;
}
// GET: api/Books
public IQueryable<BookDTO> GetBooks()
{
return UnitOfWork.BookRepository.GetBooks("").ProjectTo<BookDTO>();
}
...................
UnitOfWork: -
public class BooksRepository : IBooksRepository
{
internal BookContext Context = new BookContext();
internal DbSet<Book> dbSet;
public BooksRepository(BookContext context)
{
Context = context;
dbSet = context.Set<Book>();
}
public virtual IQueryable<Book> GetBooks(string includeProperties = "")
{
IQueryable<Book> query = dbSet;
foreach (var includeProperty in includeProperties.Split
(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
{
query = query.Include(includeProperty);
}
return query;
}
...........................
Global.asax中的我有: -
public class UnitOfWork : IDisposable, IUnitOfWork
{
private readonly BookContext _context = new BookContext();
private IBooksRepository _booksRepository;
public IBooksRepository BookRepository
{
get
{
if (_booksRepository == null)
{
_booksRepository = new BooksRepository(_context);
}
return _booksRepository;
}
}
.........................
AutoMapperConfig如下: -
AutoMapperConfig.RegisterMappings();
和BookMappings: -
public static class AutoMapperConfig
{
public static void RegisterMappings()
{
Mapper.Initialize(x =>
{
x.AddProfile<BookMappings>();
});
}
}
最后我希望运行的测试: -
public class BookMappings : Profile
{
public override string ProfileName => "BookMappings";
protected override void Configure()
{
CreateMap<Book, BookDTO>();
CreateMap<Book, BookDTO>()
.ForMember(dest => dest.BookTitle,
opts => opts.MapFrom(src => src.Title));
CreateMap<Book, BookDTO>()
.ForMember(dest => dest.AuthorName,
opts => opts.MapFrom(src => src.Author.Name));
CreateMap<Book, BookDetailDTO>();
CreateMap<Book, BookDetailDTO>()
.ForMember(dest => dest.BookTitle,
opts => opts.MapFrom(src => src.Title));
CreateMap<Book, BookDetailDTO>()
.ForMember(dest => dest.AuthorName,
opts => opts.MapFrom(src => src.Author.Name));
CreateMap<Book, BookDetailDTO>()
.ForMember(dest => dest.GenreType,
opts => opts.MapFrom(src => src.Genre.Type));
}
}
我希望将我在测试中创建的模拟列表传递给BooksController,并验证我得到了3个结果,但是我没有设法做到这一点。
目前,Get_Post_Should_Get_All_Books()的行为失败了 它似乎找不到Automapper并给出以下错误: -
System.NullReferenceException:未将对象引用设置为对象的实例。
在lambda_method(Closure,Book)
at System.Linq.Enumerable.WhereSelectListIterator [TestFixture]
public class BooksControllerTests
{
private Mock<IUnitOfWork> _unitOfWorkMock;
private Mock<IBooksRepository> _booksRepositoryMock;
BooksController objController;
List<Book> books = new List<Book>();
[SetUp]
public void Setup()
{
_unitOfWorkMock = new Mock<IUnitOfWork>();
_booksRepositoryMock = new Mock<IBooksRepository>();
_unitOfWorkMock.Setup(x => x.BookRepository)
.Returns(_booksRepositoryMock.Object);
objController = new BooksController(_unitOfWorkMock.Object);
AutoMapperConfig.RegisterMappings();
books = new List<Book>()
{
new Book {Id = 1, Title = "Hamlet", ISBN = 1853260096, AuthorId = 1, GenreId = 1, IsAvailable = true, Summary = "Hamlet is not only one of Shakespeare's greatest plays, but also the most fascinatingly problematical tragedy. "},
new Book {Id = 2, Title = "Macbeth", ISBN = 1853260355, AuthorId = 1, GenreId = 1, IsAvailable = true, Summary = "Shakespeare’s Macbeth is one of the greatest tragic dramas the world has known. "},
new Book {Id = 3, Title = "Romeo And Juliet", ISBN = 1840224339, AuthorId = 1, GenreId = 2, IsAvailable = true, Summary = "Romeo and Juliet is the world's most famous drama of tragic young love. "}
};
}
[Test]
public void Get_Post_Should_Get_All_Books()
{
// Arrange
_booksRepositoryMock.Setup(x => x.GetBooks(""))
.Returns(books.AsQueryable());
// Act
var result = objController.GetBooks().ToList();
// Assert
Assert.AreEqual(result.Count, 3);
}
}
1..ctor(IEnumerable 2.MoveNext()
at System.Collections.Generic.List
1 source)
非常感谢任何帮助。谢谢!
答案 0 :(得分:1)
您忘记配置虚假UoW(_unitOfWorkMock
)以返回存储库。
将以下行添加到您的排列或设置方法中:
_unitOfWorkMock.Setup(x => x.BookRepository)
.Returns(_booksRepositoryMock.Object);
答案 1 :(得分:0)
在阅读了一些不建议自动映射特别是TDD的帖子之后,我决定完全删除Automapper。