Click here to Skip to main content
15,891,567 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi to all..

Great day!

I would like to ask on How to Unit Test File Download in MVC 5 VB.NET ASP.NET using MOQ.

I searched already over the internet, but still didn't see any sample code.

Thank you in advance for your kind help.

Here's my repository code that need a unit testing (file download)

VB
Public Function StatusAndLocking_GetFileByProjectAttachmentID(ID As Integer) _
      As ProjectViewModel.StatusAndLockingEntity Implements IProjectRepository.StatusAndLocking_GetFileByProjectAttachmentID

      Dim result As New ProjectViewModel.StatusAndLockingEntity
      Dim anonymousIndex As IQueryable(Of Object) = Nothing
      Using db As New SPIMS_Entities()
          db.Database.Connection.ConnectionString = EFManager.GetConnectionString

          'Attachment (for download)
          Dim query = From paf In db.ProjectAttachmentFile
               Join p In db.Project On paf.ProjectID Equals p.ID
               Join pf In db.ProjectProfile On p.ProjectProfileID Equals pf.ID
               Select
                    IDAlias = paf.ID, pf.Name, pf.GIMSNumber, AttachmentNameAlias = paf.Name, paf.SystemFile

          anonymousIndex = query.Where(Function(p) p.IDAlias = ID).AsQueryable
          result = MapToProjectViewModelEntity(anonymousIndex)
      End Using
      Return result


  End Function



Here's my Controller code:

VB
'Download File
  Function GetFileByProjectAttachmentID(ByVal id As Integer) As FileContentResult
       Dim _repoProject As New ProjectRepository
      Dim _projectAttachment As New ProjectViewModel.StatusAndLockingEntity

      _projectAttachment = _repoProject.StatusAndLocking_GetFileByProjectAttachmentID(id)
      Dim fileData As Byte() = _projectAttachment.SystemFile
      Dim fileName As String = _projectAttachment.AttachmentName

      Return File(fileData, System.Net.Mime.MediaTypeNames.Application.Octet, fileName)
  End Function
Posted
Updated 11-Aug-14 16:15pm
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900