public void ShouldRetrieveAllPictures()
        {
            var mockRepository = new Mock<IPictureRepository>();

            mockRepository.Setup(r => r.GetAll()).Returns(() =>
            {
                var result = new List<Picture>();
                result.Add(new Picture());
                result.Add(new Picture());
                return result.AsQueryable();
            });

            var pictureController = new PicturesController(mockRepository.Object, null);
            var pictures = pictureController.Get();

            Assert.AreEqual(pictures.Count(), 2);
        }
        public void ShouldDeleteAPicture()
        {
            var id = Guid.NewGuid();

            var mockPictureRepository = new Mock<IPictureRepository>();
            mockPictureRepository.Setup(r => r.Get(id)).Returns(() =>
                {
                    return new Picture() { Tags = "tag1,tag2" };
                });

            var mockTagRepository = new Mock<ITagRepository>();

            var pictureController = new PicturesController(mockPictureRepository.Object, mockTagRepository.Object);
            pictureController.Delete(id);

            mockPictureRepository.Verify(r => r.Get(id), Times.Once());
            mockPictureRepository.Verify(r => r.Delete(id), Times.Once());
            mockTagRepository.Verify(r => r.DecrementPictureCount("tag1"), Times.Once());
            mockTagRepository.Verify(r => r.DecrementPictureCount("tag2"), Times.Once());
            mockTagRepository.Verify(r => r.DecrementPictureCount(It.IsAny<string>()), Times.Exactly(2));
        }
        public void ShouldRetrieveAPicture()
        {
            var id = Guid.NewGuid();

            var mockRepository = new Mock<IPictureRepository>();
            mockRepository.Setup(r => r.Get(id)).Returns(() =>
            {
                return new Picture() { Id = id };
            });

            var pictureController = new PicturesController(mockRepository.Object, null);
            var pictures = pictureController.Get(id);

            Assert.AreEqual(pictures.Id, id);
        }
        public void ShouldHaveParameterLessConstructor()
        {
            var controller = new PicturesController();

            Assert.IsInstanceOfType(controller, typeof(PicturesController));
        }
 public void ShouldThrowWhenMimeIsNotFormData()
 {
     try
     {
         var controller = new PicturesController();
         controller.Request = new HttpRequestMessage() { Content = new StreamContent(new MemoryStream()) };
         controller.Post();
     }
     catch (HttpResponseException ex)
     {
         Assert.AreEqual(ex.Response.StatusCode, HttpStatusCode.UnsupportedMediaType);
     }
 }
        public void ShouldSaveAPictureWithFileName()
        {
            var id = Guid.NewGuid();

            var mockPictureRepository = new Mock<IPictureRepository>();
            mockPictureRepository.Setup(r => r.Save(It.IsAny<Picture>(), It.IsAny<byte[]>())).Returns<Picture, byte[]>((p, i) =>
            {
                p.Id = id;
                p.Url = new Uri("http://foo", UriKind.Absolute);
                return p;
            });

            var mockTagRepository = new Mock<ITagRepository>();

            var pictureController = new PicturesController(mockPictureRepository.Object, mockTagRepository.Object);

            var content = new StreamContent(File.OpenRead(Path.Combine(Environment.CurrentDirectory, "multipartStreamEmpty.txt")));
            content.Headers.Add("Content-Type", "multipart/form-data; boundary=---------------------------7dcfd134057c");
            pictureController.Request = new HttpRequestMessage() { Content = content };

            var picture = pictureController.Post();

            Assert.AreEqual(picture.Id, id);
            Assert.AreEqual(picture.Url.ToString(), "http://foo/");
            Assert.AreEqual(picture.Name, "test.txt");
            Assert.IsNull(picture.Description);
            Assert.IsNull(picture.Tags);

            mockTagRepository.Verify(r => r.IncrementPictureCount(It.IsAny<string>()), Times.Never());
        }