Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Rhino Mock an IEnumerable<HttpPostedFileBase> anyone?

Posted on 2013-07-01
5
Medium Priority
?
707 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 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses

783 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