Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Rhino Mock an IEnumerable<HttpPostedFileBase> anyone?

Posted on 2013-07-01
5
Medium Priority
?
698 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
5 Comments
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 2000 total points
ID: 39292089
What is the test you are trying to perform?
0
 

Author Comment

by:sbornstein2
ID: 39292763
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
ID: 39292766
i just need to be able to call the method for now passing the param.
0
 

Author Comment

by:sbornstein2
ID: 39293104
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
ID: 39293149
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

Enroll in September's Course of the Month

This month’s featured course covers 16 hours of training in installation, management, and deployment of VMware vSphere virtualization environments. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

721 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