Returns won't return null

6 posts, 1 answers
  1. Imaji
    Imaji avatar
    15 posts
    Member since:
    Apr 2007

    Posted 23 Jun 2010 Link to this post

    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?
  2. Answer
    Ricky
    Admin
    Ricky avatar
    467 posts

    Posted 23 Jun 2010 Link to this post

    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
  3. DevCraft R3 2016 release webinar banner
  4. Imaji
    Imaji avatar
    15 posts
    Member since:
    Apr 2007

    Posted 25 Jun 2010 Link to this post

    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
  5. Ricky
    Admin
    Ricky avatar
    467 posts

    Posted 25 Jun 2010 Link to this post

    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
  6. Gar
    Gar avatar
    1 posts
    Member since:
    Apr 2011

    Posted 14 Jun 2011 Link to this post

    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())
  7. Ricky
    Admin
    Ricky avatar
    467 posts

    Posted 17 Jun 2011 Link to this post

    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
Back to Top
DevCraft R3 2016 release webinar banner