public void MapReturnsPostViewModel()
        {
            // Arrange
            DateTime referenceDate = DateTime.Now;
            Author author = new Author {AuthorID = 1, Name = "Fred"};
            Category category = new Category {CategoryID = 1, Name = "Cat"};
            Guid externalIdentifier = Guid.NewGuid();
            Post post = new Post
                            {
                                Author = author,
                                AuthorID = 1,
                                Body = "Some text",
                                Categories = new List<Category> {category},
                                ExternalIdentifier = externalIdentifier,
                                PostID = 2,
                                PostedAt = referenceDate,
                                Subject = "A subject"
                            };
            _categoryMapper.Stub(x => x.Map(category)).Return(new CategoryViewModel());
            _markdownConverter.Stub(x => x.ToHtml("Some text")).Return("converted");
            PostToPostViewModelMapper mapper = new PostToPostViewModelMapper(_categoryMapper, _markdownConverter);

            // Act
            PostViewModel result = mapper.Map(post);

            // Assert
            Assert.That(result.AuthorName, Is.EqualTo("Fred"));
            Assert.That(result.AuthorId, Is.EqualTo(1));
            Assert.That(result.BodyAsHtml, Is.EqualTo("converted"));
            Assert.That(result.Categories.Count, Is.EqualTo(1));
            Assert.That(result.ExternalIdentifier, Is.EqualTo(externalIdentifier));
            Assert.That(result.PostID, Is.EqualTo(2));
            Assert.That(result.PostedAt, Is.EqualTo(referenceDate));
            Assert.That(result.Subject, Is.EqualTo("A subject"));
        }
        public void MapsToCategoryViewModel()
        {
            // Arrange
            CategoryToCategoryViewModelMapper mapper = new CategoryToCategoryViewModelMapper();
            Category category = new Category {CategoryID = 1, Name = "Cat"};

            // Act
            CategoryViewModel result = mapper.Map(category);

            // Assert
            Assert.That(result.CategoryID, Is.EqualTo(1));
            Assert.That(result.Name, Is.EqualTo("Cat"));
        }
        public void AllReturnsCategoriesInOrder()
        {
            // Arrange
            CategoriesService service = new CategoriesService(_unitOfWorkFactory, _mapper);
            Category category1 = new Category {CategoryID = 1, Name = "zoo"};
            Category category2 = new Category {CategoryID = 2, Name = "balloon"};
            List<Category> categories = new List<Category> {category1, category2};
            CategoryViewModel model1 = new CategoryViewModel {CategoryID = 1};
            CategoryViewModel model2 = new CategoryViewModel {CategoryID = 2};
            _mapper.Stub(x => x.Map(category1)).Return(model1);
            _mapper.Stub(x => x.Map(category2)).Return(model2);
            _repository.Stub(x => x.All).Return(categories.AsQueryable());

            // Act
            IEnumerable<CategoryViewModel> models = service.All();

            // Assert
            Assert.That(models.First(), Is.EqualTo(model2));
            Assert.That(models.Last(), Is.EqualTo(model1));
        }
        public void Setup()
        {
            _category1 = new Category {CategoryID = 1};
            _category2 = new Category {CategoryID = 2};
            _post1 = new Post { PostID = 1, PostedAt = DateTime.Now.Subtract(TimeSpan.FromDays(50)), Categories = new Collection<Category>{_category1}};
            _post2 = new Post { PostID = 2,  PostedAt = DateTime.Now, Categories = new Collection<Category>{ _category2}};
            List<Post> posts = new List<Post> { _post1, _post2 };

            _unitOfWorkFactory = MockRepository.GenerateStub<IUnitOfWorkFactory>();
            _unitOfWork = MockRepository.GenerateStub<IUnitOfWork>();
            _postRepository = MockRepository.GenerateStub<IRepository<Post>>();
            _mapper = MockRepository.GenerateStub<IMapper<Post, PostViewModel>>();

            _unitOfWorkFactory.Stub(x => x.Create()).Return(_unitOfWork);
            _unitOfWork.Stub(x => x.GetRepository<Post>()).Return(_postRepository);
            _unitOfWork.Stub(x => x.Execute(Arg<Action>.Is.Anything)).WhenCalled(x => ((Action)x.Arguments[0])());

            _postRepository.Stub(y => y.AllIncluding(Arg<Expression<Func<Post, object>>>.Is.Anything)).Return(posts.AsQueryable());
        }