Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Investigate possibility to infer "real" type of substitute to improve non-virtual member calls detection #68

Open
tpodolak opened this issue Feb 11, 2019 · 1 comment
Labels
enhancement New feature or request

Comments

@tpodolak
Copy link
Member

As reported in here nsubstitute/NSubstitute#499 (comment) NSubstitute.Analyzers fails to find non-virtual member calls when substituting for class but assigning it to interface e.g

IUnitOfWorkCoreCqrs cqrsUow = Substitute.ForPartsOf<CqrsUow>();
cqrsUow.Received(1).RegisterAfterCommitCallBackItem(Arg.Any<Func<Task>>());

In order to correctly report non-virtual member calls, we have to find actual substitute assignment

IUnitOfWorkCoreCqrs cqrsUow = Substitute.ForPartsOf<CqrsUow>();

or

IUnitOfWorkCoreCqrs cqrsUow = null;
cqrsUow  = Substitute.ForPartsOf<CqrsUow>();

based on the type infered from expression, we should be able to enhance CanBeSetuped method.

Possible roslyn API to use

var dataFlowAnalysis = syntaxNodeContext.SemanticModel.AnalyzeDataFlow(accessedMember);

Possible problems

  • performace degradation, we might decide not to implement it
@dtchepak
Copy link
Member

Thanks for tracking this issue.

This gets even more fun if we have a IUnitOfWorkCoreCqrs field that gets modified by multiple threads and is sometimes assigned instances of different types. 😂

There may be some simple cases we can identify (local variable, single assignment?), but my initial feeling is that it may not be worth the cost (finding a reliable implementation, plus the performance overhead). Happy to be proven wrong though! :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants