示例#1
0
        public static ITorrentRepository GetTorrentRepository()
        {
            var repositoryMock = new Mock <ITorrentRepository>();
            var torrents       = new DbQueryMock <Torrent>(DataInitializer.GeTorrents()).Object;

            repositoryMock.Setup(x => x.GetAll()).Returns(torrents);

            repositoryMock.Setup(x => x.GetAll(It.IsAny <Expression <Func <Torrent, bool> > >()))
            .Returns <Expression <Func <Torrent, bool> > >(torrents.Where);

            repositoryMock.Setup(x => x.GetAsync(It.IsAny <int>()))
            .ReturnsAsync((int id) => torrents.SingleOrDefault(x => x.Id == id));

            repositoryMock.Setup(x => x.GetAsync(It.IsAny <Expression <Func <Torrent, bool> > >()))
            .ReturnsAsync((Expression <Func <Torrent, bool> > expression) => torrents.SingleOrDefault(expression));

            repositoryMock.Setup(x => x.CountAsync()).ReturnsAsync(torrents.Count);

            repositoryMock.Setup(x => x.CountAsync(It.IsAny <Expression <Func <Torrent, bool> > >()))
            .ReturnsAsync((Expression <Func <Torrent, bool> > expression) => torrents.Count(expression));

            repositoryMock.Setup(x => x.ExistAsync(It.IsAny <int>()))
            .ReturnsAsync((int id) => torrents.Any(x => x.Id == id));

            return(repositoryMock.Object);
        }