public void ItShouldGetOneAttachment()
        {
            var mockProxy = new Mock<ITFSAttachmentProxy>();

            var expectedResult = new Attachment
            {
                Id = "29161-0",
                WorkItemId = 29161,
                Index = 0,
                Name = "Sample File 1.txt",
                Extension = "txt",
                Comment = "Sample Comment 1",
                Length = 1024,
                AttachedTime = DateTime.Now,
                CreationTime = DateTime.Now,
                LastWriteTime = DateTime.Now,
                Uri = "http://sampleuri/"
            };

            mockProxy.Setup(p => p.GetAttachment(It.Is<int>(id => id == 29161), It.Is<int>(index => index == 0)))
                .Returns(expectedResult)
                .Verifiable();

            var repository = new AttachmentRepository(mockProxy.Object);

            var result = repository.GetOne("29161-0");

            Assert.AreEqual(result, expectedResult);
            mockProxy.VerifyAll();
        }
        public void CreateAttachment(Attachment attachment)
        {
            if (attachment == null)
            {
                throw new ArgumentNullException("attachment");
            }

            var wiql = string.Format(CultureInfo.InvariantCulture, "SELECT [System.Id] FROM WorkItems WHERE [System.Id] = {0}", attachment.WorkItemId);

            var workItem = this.QueryWorkItems(wiql)
                               .Cast<TeamFoundation.WorkItemTracking.Client.WorkItem>()
                               .FirstOrDefault();

            if (workItem == null)
            {
                throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, "There is not any Work Item with an Id equals to {0}", attachment.WorkItemId), "attachment.WorkItemId");
            }

            var path = Path.Combine(Path.GetTempPath(), attachment.Id);
            var fileInfo = new FileInfo(path);

            if (!string.IsNullOrWhiteSpace(attachment.Name))
            {
                var fileName = string.IsNullOrWhiteSpace(attachment.Extension)
                                ? attachment.Name
                                : attachment.Extension.StartsWith(".", StringComparison.OrdinalIgnoreCase)
                                    ? string.Format(CultureInfo.InvariantCulture, "{0}{1}", attachment.Name, attachment.Extension)
                                    : string.Format(CultureInfo.InvariantCulture, "{0}.{1}", attachment.Name, attachment.Extension);

                path = Path.Combine(Path.GetTempPath(), fileName);

                if (File.Exists(path))
                {
                    File.Delete(path);
                }

                fileInfo.MoveTo(path);
            }

            workItem.Attachments.Add(attachment.ToEntity(path));
            workItem.Save();

            fileInfo.Delete();
        }