Solved

Rhino Mock an IEnumerable<HttpPostedFileBase> anyone?

Posted on 2013-07-01
5
686 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 500 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

Suggested Solutions

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.

759 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