-1

I am struggeling with writing unit tests for my class AttachmentProcessor. My goal is to test all methods and mock for example IList FileList.

 public class AttachmentsProcessor { public IList<IFileInfo> FileList { get; set; } public AttachmentsProcessor(IList<IFileInfo> FileList) { ... } public void RemoveAttachment(int index) { ... } public long GetTotalFilesSize() { ... } public void GetFilesFromDialog(IOpenFileDialog2 openFileDialog1) { ... } } 

Interface :

public interface IFileProcessor { IList<IFileInfo> FileList { get; set; } void RemoveAttachment(int index); long GetTotalFilesSize(); void GetFilesFromDialog(IOpenFileDialog2 openFileDialog1); } 
5
  • 3
    Why are you unit testing that in the first place? You're just wrapping a list for convience methods, you don't even need that method in the first place, Secondly AttachmentsInfo may be a IFileInfo, but not vice versa so if you want to act upon it like so your initial call must be AttachmentsProcessor<IFileInfo> but if you're going to do that you should use a structure better suited to handle co-variance and contravariance Commented Mar 21, 2017 at 15:37
  • Quick question - what is benefit of using AttachmentProcessor over using simple List<AttahcmentsInfo>? Commented Mar 21, 2017 at 15:57
  • @SergeyBerezovskiy I have methods like GetSizeOfAllAttachments, GetFilesFromDialog, or GetFilesFromDragandDrop Event etc. Commented Mar 22, 2017 at 8:29
  • @johnny5 thank you for your reply ...it makes sense. But I still don't know how to mock a List<AttachmentInfo>. I just want to moq the AttachmentInfo objects in that list. Commented Mar 22, 2017 at 8:30
  • I don't understand what you're trying to do. Post a function you're trying to moq. You are aware that you don't have to moq everything sometimes you just need to make. Fake dto Commented Mar 22, 2017 at 12:47

1 Answer 1

0

I've solved my problem. My problem was that I did not know how to make mocks for my Class AttachmentProcessor:IFileProcessor and List. I ended up with this solution and it worked ! Thanks for all replies.

 var mockFileProcessor = new Mock<IFileProcessor>(); //here is how I mocked my AttachmentProcessor var mockAttachmentInfo = new Mock<IFileInfo>(); ///here is how I mocked my AttachmentInfo mockAttachmentInfo.Setup(m => m.Length).Returns(() => 200); mockFileProcessor.Setup(m => m.FileList).Returns(() => new List<IFileInfo> { mockAttachmentInfo.Object, mockAttachmentInfo.Object, mockAttachmentInfo.Object, mockAttachmentInfo.Object, mockAttachmentInfo.Object }); /// and this is the part where I mocked my IList<AttachmentInfo> 

I ended up with this solution and it worked ! Thanks for all replies

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.