Solved

Rhino Mock an IEnumerable<HttpPostedFileBase> anyone?

Posted on 2013-07-01
5
658 Views
Last Modified: 2013-07-12
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

0
Comment
Question by:sbornstein2
  • 4
5 Comments
 
LVL 74

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
Comment Utility
What is the test you are trying to perform?
0
 

Author Comment

by:sbornstein2
Comment Utility
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
0
 

Author Comment

by:sbornstein2
Comment Utility
i just need to be able to call the method for now passing the param.
0
 

Author Comment

by:sbornstein2
Comment Utility
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.
0
 

Author Comment

by:sbornstein2
Comment Utility
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

0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now