DarthVader
DarthVader

Reputation: 55022

How can i mock HttpPostedFileBase mock

How can I mock HttpPostedFileBase ?

its props are readonly and i need to test a fileupload module.

What mocking framework should i use?

    public class FileUpload
    {

        private readonly HttpPostedFileBase _file;
        private readonly string _filePath;

        public FileUpload(HttpPostedFileBase file, string path)
        {
            _file = file;
            _filePath = path;
        }
.......

Upvotes: 0

Views: 664

Answers (1)

Jeff Foster
Jeff Foster

Reputation: 44706

HttpPostedFileBase is an abstract class. In your test, just create a new class inheriting from it and create your own stub version (hardcoding the method results you need to test). What style of testing are you doing?

  • mocking (testing for interaction)
  • stubbing (testing with known values)
  • fakes (using simpler objects to accomplish the task, not necessarily production code).

I think here stubbing would be most appropriate.

Alternatively, you could use Moq (or another framework) to help with this.

Upvotes: 2

Related Questions