This is a migrated thread and some comments may be shown as answers.

Returns won't return null

7 Answers 307 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Imaji
Top achievements
Rank 1
Imaji asked on 23 Jun 2010, 12:32 PM
In a couple of my tests, I want to prove that the calling code can handle a null being returned, but whenever I tell the Returns method to return null, I get an instance of the returned type.

So for example lets says I have a method signature like:

public interface ExampleInterface
{
IList<IBob> GetMeAllTheBobs(ISomeParameter someParameter)
}

When I mock like so:

var stubExampleClass = Mock.Create<IExampleClass>();
Mock.Arrange(() => stubExampleClass.GetMeAllThyeBobs(Arg.Any<ISomeParameter>()).Returns(null);

I will get an instance of IList<IBob> back instead of null.

Any suggestions?

7 Answers, 1 is accepted

Sort by
0
Accepted
Ricky
Telerik team
answered on 23 Jun 2010, 05:03 PM
Hi John,

Thanks for the post, i tested your code , yes there were some issue with Justmock setting up null value for List/Dictionary. You can tell  me your email address i can send you a build personally if it is too urgent for you.

However, i would like to inform you that returning null for list is a design violation according to frameowrk design guidelines. You might like to check this:
http://stackoverflow.com/questions/1969993/is-it-better-to-return-null-or-empty-collection


Also, you can't return null directly  like one you mentioned:
Mock.Arrange(() => stubExampleClass.GetMeAllThyeBobs(Arg.Any<ISomeParameter>()).Returns(null);

Rather you will have to do:
Mock.Arrange(() => stubExampleClass.GetMeAllThyeBobs(Arg.Any<ISomeParameter>()).Returns((IList<Bob>)null);

The previous one acutally wont build as there are other overloads and you need to tell the compiler what overload you acutally want to pick up and set null.  Our of curiousity, i found an interesting issue simiilar posted in a forum at google groups for Moq.
http://code.google.com/p/moq/issues/detail?id=167


Hope that helps,
Mehfuz

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Imaji
Top achievements
Rank 1
answered on 25 Jun 2010, 08:33 AM
I agree that a list should be returned by the code I consume, but I can't guarantee that :)  So I'm being paranoid in my code just to be on the safe side!

I'll pass on the test build for the tmie being thanks.  Is there an ETA for Beta 2, RC, or RTM?

Thanks,
J
0
Ricky
Telerik team
answered on 25 Jun 2010, 04:15 PM
Hi John,

The build / release is scheduled for early july. Of course in between, if  you find more issues just let  us know or shoot me an email at mehfuz.hossain@telerik.com

Regards,
Mehfuz


Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Gar
Top achievements
Rank 1
answered on 15 Jun 2011, 02:51 AM
I'm trying to arrange a method such that it returns null.  The method returns an Object array.  However when it is called it returns an array instance of length = 0. 

I've tried various other ways to mock the return but nothing seems to work other than changing the return type of the method.  The method returns an object array.  If I change the return type to something like an IList(of Object), Mock.Arrange seems to let me return null, but this isn't really a change I want to make.  Any ideas?


Mock.Arrange(Of
Object())(Function() mockedInstance.Read()).Returns(DirectCast(Nothing, Object()))
Assert.IsNull(mockedInstance.GetValues())
0
Ricky
Telerik team
answered on 17 Jun 2011, 03:37 PM
Hi Gar,
Thanks again for sending the issue.

Yes we realized that it should not return default value when set explicitly. However it is fixed now and should you require an internal build please let me know. (In that regard, please open up s support ticket  since we can't send internal builds on public forum). 

Alternatively, we are planning for a patch on this coming Monday which also contains the fix.

Finally about the empty array, we follow framework design guidelines for returning default value for collection or array:

From the Framework Design Guidelines 2nd Edition [1] (pg. 256):
DO NOT return null values from collection properties or from methods returning collections. Return an empty collection or an empty array instead.




Kind Regards,
Mehfuz
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Nicolas
Top achievements
Rank 1
answered on 05 Oct 2020, 05:18 PM
I notice that this isn't active for almost 9 years....

I had this situation. I needed to test the managing that I was doing to null responses and I had something like this:

 
ObjectDto objDto = null;
Mock.Arrange(() => _service.GetObjDto(someParameter, _cancellationToken))<br>                    .Returns(Task.FromResult(objDto));


But with, still is returning an instance of ObjectDto.
I solvedtryed to use 

 
   Mock.Arrange(() => _service.GetLockAsync(Guid.NewGuid(), _cancellationToken))                                    .Returns(Task.FromResult<ObjectLockDto>(null));

 

but still getting me the empty instance instead the null so I changed for 

 

Mock.Arrange(() => _service.GetLockAsync(Arg.IsAny<Guid>(), Arg.IsAny<CancellationToken>()))                .Returns(Task.FromResult<ObjectLockDto>(null));

 

0
Ivo
Telerik team
answered on 06 Oct 2020, 10:54 AM

Hello Nicolas,

The arrangements you are using should behave in the same way, the only difference is the scope that they are applicable to - the first two of them are applicable if there is an exact match of the given parameters in the arrangement expression, the last one - for any possible values of the parameters. I have made a sample project and played a little bit with this use case, but I am unable to observe such a problem, probably I have missed something. Please use the attached sample and modify it to match the issue and send it back to us for further investigation.

Regards,
Ivo
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Tags
General Discussions
Asked by
Imaji
Top achievements
Rank 1
Answers by
Ricky
Telerik team
Imaji
Top achievements
Rank 1
Gar
Top achievements
Rank 1
Nicolas
Top achievements
Rank 1
Ivo
Telerik team
Share this question
or