-
Notifications
You must be signed in to change notification settings - Fork 264
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
Argument matcher for a struct with a field with non-default value #766
Comments
@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 NSubstitute/src/NSubstitute/Core/Arguments/ArgumentSpecificationCompatibilityTester.cs Lines 12 to 22 in f470b5d
we compare the provided argument with its default value NSubstitute/src/NSubstitute/Core/DefaultForType.cs Lines 29 to 50 in f470b5d
Where we use It looks like a fix would be to propagate the 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? |
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. |
Hi @alexandrnikitin 👋 😄
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 |
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;
} |
Also having issues with value types generated by Vogen. |
Hi - I've got a bug report in Vogen about this. If I was to take a look at fixing it in NSubstitute, are there any pointers on how to start, e.g. testing etc.? |
Yes you can use my minimal reproduction above |
Thanks, I was more looking for some pointers on what areas to look at in nsub itself |
Describe the bug
I am suspecting that something is not working well with argument matchers that matches structs with a field with non-default value initializer. Maybe I am doing something wrong - int this case please point me to the right direction :)
To Reproduce
Expected behaviour
I think is should just match provided object's instance. Instead of that I am getting exception below:
To get rid of the exception I can do one of the things below:
Environment:
Additional context
I reproduced this case with the Moq library and it works fine.
Argument matcher for a struct with a field with non-default value
The text was updated successfully, but these errors were encountered: