How to return mock object as a null

krypru

as the topic says I don't know how to return a mocked object as null in my MVC Testing project. I'm new on making unit tests.

I have an action:

[HttpPost]
public ActionResult Edit(ClubToAddVM  clubToAddVm, HttpPostedFileBase imageFile)
{
    if (ModelState.IsValid)
    {
        if (imageFile!=null)
        {
            clubToAddVm.ImageMimeType = imageFile.ContentType;
            clubToAddVm.ImageData = new byte[imageFile.ContentLength];
            imageFile.InputStream.Read(clubToAddVm.ImageData, 0, imageFile.ContentLength);
        }
    }
    ...
}

And I want in my test to pass the imageFile object as a null. Unfortunately I can't create instance of HttpPostedFileBase abstract class and I wanna try with something like this:

var mockImageFile = new Mock<HttpPostedFileBase>();

But then I don't know how to make it as null, because mockImageFile.Object is readonly. Any ideas?

Erik Schierboom

You don't have to create an instance if you want to make it null, just do:

HttpPostedFileBase imageFile = null;

That it is an abstract class does indeed mean that you cannot create an instance of it, but it is perfectly fine to declare a variable of that type and set it to null.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Why does the getter() of mock object return null?

From Dev

@Mock object instance is null

From Dev

How can I make Ninject kernel return NSubstitute mock object?

From Dev

How to mock a return value?

From Dev

How to return empty string if an object is null?

From Dev

How to mock a CellSet object

From Dev

How to mock an Object in Scala

From Dev

How to mock with Json Object

From Dev

How to mock object in EntityManager?

From Dev

Expression causes mock repository to return null

From Dev

return null Parcelable object

From Dev

Return object not null but empty

From Dev

Return deferred object or null

From Dev

How to return the "mock implementation" of a singleton?

From Dev

How to mock a socket object via the mock library

From Dev

How to mock a socket object via the mock library

From Dev

Mock property return value gets overridden when instantiating mock object

From Dev

Jasmine have createSpy() return mock object

From Dev

When unit testing, how do I mock a return null from async method?

From Dev

Powermock keeps returning me a null object not a mock

From Dev

How to mock an angular $http call and return a promise object that behaves like $http

From Dev

How can I get mock a method to only return an object once? (I am testing a pagination cursor)

From Dev

Php, how to mock object instantiation?

From Dev

How to verify that mock object was "gotten"?

From Dev

How to mock a Kotlin singleton object?

From Dev

How to mock a new object in mockito

From Dev

How to Mock an object with protected init

From Dev

SHOULD function return Null or Object of it?

From Dev

java object instantiation to return null?

Related Related

HotTag

Archive