Link to home
Start Free TrialLog in
Avatar of sbornstein2
sbornstein2

asked on

Rhino Mock an IEnumerable<HttpPostedFileBase> anyone?

Hello all,

I am trying to write a unit test where in my solution I added to my appdata folder a pdf file.   I have the following method in my MVC project and I need to see an example of using Rhino Mock to test the file upload.   I also want to make sure that I don't have to hardcode a physical path such as "C:\Project\AppData" as each user may have a different working folder path.   Just want to use the pathing within the solution I guess to the appdata folder sample.pdf file.   Thanks all below is the method params coming in the controller.

 public ActionResult FileUpload(IEnumerable<HttpPostedFileBase> files, string library, string folder)
        {
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of sbornstein2
sbornstein2

ASKER

uploading a file to sharepoint but i just need a sample to get the file into the ienumerable for post and where it can path to it in the appdata folder
i just need to be able to call the method for now passing the param.
kaufmed you raise a good question actually.  I was thinking your right what am I actually trying to 'unit test' versus integration testing.   So what I am doing is I use the Microsoft.Sharepoint.Client dll's in my ASP.Net MVC project.   Then I use Caml Query to for example upload a file to the sharepoint site url I have into a library or remove a file, get files etc.   I have three methods to handle these functions.   The way I was thinking was to actually upload a file to the sharepoint site but you got me thinking what will I actually be trying to assert.   Saving an actual file to the site etc. is more integration or behavioral testing of the actual functionality.    So that begs the question now for me what will I want to actually unit test and assert and I will need to think about that.
Here is my file upload method:

   public DocumentServiceResponse FileUpload(IEnumerable<HttpPostedFileBase> files, string library, string folder)
        {
            var response = new DocumentServiceResponse();

            try
            {
                if (files != null)
                {
                    var list = _spContext.Web.Lists.GetByTitle(library);

                    foreach (var file in files)
                    {
                        var fsReader = new StreamReader(file.InputStream);
                        byte[] contents;
                        using (var fStream = fsReader.BaseStream)
                        {
                            contents = new byte[fStream.Length];
                            fStream.Read(contents, 0, (int)fStream.Length);
                            fStream.Close();
                        }

                        var newFile = new FileCreationInformation
                            {
                                Content = contents,
                                Url = Path.GetFileName(file.FileName),
                                Overwrite = true
                            };

                        File uploadingFile = null;

                        if (folder != null)
                        {
                            var query = new CamlQuery {ViewXml = "<View/>"};

                            var folderUrl = String.Format("/{0}/{1}", library, folder);
                            query.FolderServerRelativeUrl = folderUrl;

                            var folders = list.RootFolder.Folders;
                            var existingFolders = _spContext.LoadQuery<Folder>(
                                folders.Where(
                                f => f.ServerRelativeUrl == query.FolderServerRelativeUrl)
                                );
                            _spContext.ExecuteQuery();
                            var existingFolder = existingFolders.FirstOrDefault();
                            if (existingFolder != null)
                            {
                                uploadingFile = existingFolder.Files.Add(newFile);
                            }
                        }
                        else
                        {
                            uploadingFile = list.RootFolder.Files.Add(newFile);
                        }

                        _spContext.Load(uploadingFile);
                        _spContext.ExecuteQuery();
                    }
                }
 
                response.ResultType = ServiceResultType.Success;
            }
            catch
            {
                response.ResultType = ServiceResultType.ExceptionThrown;
            }

            return response;
        }

Open in new window