Comments (4)
@eValker Thank you for the issue and the great repro. Yes, that's a bug indeed.
It happens because when we check the provided argument specification in
we compare the provided argument with its default value
NSubstitute/src/NSubstitute/Core/DefaultForType.cs
Lines 29 to 50 in f470b5d
Where we use Activator.CreateInstance
to create an instance of the specified type. It in its turn calls the default constructor of the struct which initializes the field with the default value not the specified one. That's why it doesn't consider the provided argument spec and throws RedundantArgumentMatcherException
It looks like a fix would be to propagate the matchArgs
from ArgumentSpecificationsFactory
down to the call stack and don't compare default values if it's MatchArgs.Any
https://github.com/nsubstitute/NSubstitute/blob/f470b5de58a0bb1f41157b7ce6210e35dd4f977f/src/NSubstitute/Core/Arguments/ArgumentSpecificationsFactory.cs#L49C16-L51
But I don't remember that part of code and frankly don't understand why we need to compare the provided spec with the default value of its type. @dtchepak @zvirja maybe you remember that? Is there a better fix?
from nsubstitute.
I am specifically encountering this behavior as well while exploring strongly typed ids (defined as structs wrapping the supporting value) in my code base.
It's not obvious if there is a work-around without changing how we would model these values.
from nsubstitute.
Hi @alexandrnikitin 👋 😄
But I don't remember that part of code and frankly don't understand why we need to compare the provided spec with the default value of its type. @dtchepak @zvirja maybe you remember that? Is there a better fix?
IIRC we looked at default values to try to match up enqueued arg matchers with the actual args passed to members. (I'm not sure what the other options are there?)
The instance we return from Arg.*
uses DefaultValueContainer
. Can we use the same mechanism instead of Activator.CreateInstance
?
from nsubstitute.
I've run across the same issue and I have attached a similar repro scenario
public class Tests
{
[Test]
public void Test1()
{
var service = Substitute.For<IService>();
service.Query(Arg.Any<RequestDto>())
.Returns(new ResponseDto());
var result = service.Query(new RequestDto());
}
}
public interface IService
{
Task<ResponseDto> Query(RequestDto request);
}
public class SimpleService : IService
{
public Task<ResponseDto> Query(RequestDto request)
{
return Task.FromResult(new ResponseDto());
}
}
public struct RequestDto()
{
public string Request { get; init; } = string.Empty;
}
public class ResponseDto
{
public string Response { get; set; } = string.Empty;
}
The solution to get around this was to swap the struct to a class
public class RequestDto
{
public string Request { get; init; } = string.Empty;
}
from nsubstitute.
Related Issues (20)
- IMemoryCache TryGetValue mocking not working HOT 2
- Unable to match arguments whose type is generic, when their concrete type is not known HOT 1
- Arg.Any<Arg.AnyType>() does not match arguments passed by reference
- Provide a way to pass null for custom event args so that Raise.EventWith can be used to raise events for custom event args without a default constructor
- I can't test code that has dbcontext.SomeEntity.SingleOrDefault() HOT 2
- I'm receiving NSubstitute.Exceptions.NotASubstituteException when I shouldn't HOT 3
- Need to release 5.2.0 HOT 8
- Configured calls to 'hidden' properties are not matched HOT 2
- Improve output for expected argument for custom argument matchers on non-match or make it customizable HOT 8
- Open issues that could be closed? HOT 8
- Ability to mock requests from Protected methods in sequences HOT 1
- Proposal: Replace Google groups with GitHub discussions HOT 7
- .When(...).Throws gives a CouldNotSetReturnDueToNoLastCallException HOT 1
- ReceivedCallsException despite the expected and actual calls being identical. HOT 4
- GetArguments is returning the latest call for all calls if we're using the same reference type HOT 3
- Uncommented code causes unrelated test to pass (NSubstitute version: 4.18.1) HOT 2
- Received calls method a second time. HOT 2
- Clear configured call HOT 3
- Proposal: Change Arg.Is(Expression<Predicate<T>>) to Arg.Is(Predicate<T>) HOT 1
- Precompile error during Received assertion HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from nsubstitute.