question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Common default return value of functions with generics

See original GitHub issue

I am writing unit tests for little tasks that are talking to the database. My test cases also include scenarios where a given object is not found in the database, and therefore I’d like to emulate that the queries return null.

Now, I managed to achieve this by using the following calls:

var _session = // database session
A.CallTo(_session).WithReturnType<Foo>().Returns(null);
A.CallTo(_session).WithReturnType<Bar>().Returns(null);
// ~10 more of these with just different types defined

however I was wondering if there is a way of setting this up as the default behaviour - so that all calls to _session with a generic parameter will return null by default?

I don’t know if it is possible in the first place, but it would probably be a nice feature.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:27 (26 by maintainers)

github_iconTop GitHub Comments

2reactions
thomaslevesquecommented, Jun 16, 2016

What shall we do about the analyzer? I don’t think we need a separate issue, we can just say that this feature complete with docs, analyzer warning, etc.

Yes, I’ll reopen and submit a PR with the analyzer changes.

1reaction
blairconradcommented, Apr 11, 2016

To expand, the way I thought @thomaslevesque’s suggestion would work would be

A.CallTo(fake).Where(…).WithNonVoidReturnType().Returns(null);

(or swap Where and WithNonVoidReturnType, as desired, like we can now with Where and WithReturnType<>)

Read more comments on GitHub >

github_iconTop Results From Across the Web

Return default value for generic type
3 Answers. You can't return nil for any type. If int is used as the type argument for T for example, returning nil...
Read more >
28.1.6.1. Introduction to Generic Functions
Generic functions are true functions that can be passed as arguments, returned as values, used as the first argument to funcall and apply,...
Read more >
How To Use Generics in TypeScript
In this code, you are creating a function called someFunction , which returns true . You are then using the typeof operator to...
Read more >
Documentation - Generics
The identity function is a function that will return back whatever is passed in. ... You can read the type of loggingIdentity as...
Read more >
Generics - The Move Book
Generics are commonly used in library code, such as in vector, to declare code ... function that takes a value of any type...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found