I suggest you ...

Expose IDefaultValueProvider?

For example, if I want to override a loose mock to also return empty lists or a dummy object for a specific type.

Currently, I've set up a mock factory class that will use reflection on the interface after the mock has been created. If a method returns a type that I'd like a different default value for, it uses reflection to call Setup using that method and returning the empty list / dummy object. It works, but it's really slow.

2 votes
Vote
Sign in
Check!
(thinking…)
Reset
or sign in with
  • facebook
  • google
    Password icon
    I agree to the terms of service
    Signed in as (Sign out)
    You have left! (?) (thinking…)
    Tinister shared this idea  ·   ·  Admin →
    under review  ·  Adminkzu (Admin, moq) responded  · 

    It also returns empty for enumerables and iqueryables.
    if you set the default value to DefaultValue.Mock, you’d get an empty list too. Have you tried that?

    4 comments

    Sign in
    Check!
    (thinking…)
    Reset
    or sign in with
    • facebook
    • google
      Password icon
      I agree to the terms of service
      Signed in as (Sign out)
      Submitting...
      • Adminkzu (Admin, moq) commented  · 

        Ok, so basically you want us to add List<T> and maybe Collection<T> too to return empty collections rather than null?

      • Tinister commented  · 

        Hmm, I'd still like for a loose mock to return nulls for business objects, while returning empty lists. MockDefaultValueProvider will attempt to call the default constructor to create the business objects, yes?

      • Tinister commented  · 

        Yes, I am familiar with the DefaultValue property. The problem is, though, if you pass in DefaultValue.Empty, it only returns an empty enumerable for _arrays_. A method that returns List<T> will return null.

        And I think exposing but keeping it undocumented would be a fantastic idea. Thanks!

      • Adminkzu (Admin, moq) commented  · 

        I assume you know how to change the DefaultValue property on mocks (which would make returning empty lists and mock objects very easy).

        Would it be enough if the interface (and corresponding Mock<T> property) were exposed but undocumented and hidden from intellisense? (I think they complicate the API for the majority of users)

      Feedback and Knowledge Base