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

Callback not fired for void when Arg.Any passed #484

Closed
mkolumb opened this issue Nov 15, 2018 · 2 comments
Closed

Callback not fired for void when Arg.Any passed #484

mkolumb opened this issue Nov 15, 2018 · 2 comments

Comments

@mkolumb
Copy link

mkolumb commented Nov 15, 2018

Describe the bug
I am using the Fluent Assertions for clear assertions but it doesn't affect this bug.

I have interface with the following method

void ReduceProductAvailability(int productId, int orderedQuantity);

When I create the substitute which throws exception for this

var service = Substitute.For<IProductService>();

service
    .When(e => e.ReduceProductAvailability(Arg.Any<int>(), Arg.Any<int>()))
    .Do(x => throw new NotFoundException("XYZ"));

It not throws when in execution I use

service.ReduceProductAvailability(Arg.Any<int>(), Arg.Any<int>())

but it works if I use simple parameters

service.ReduceProductAvailability(0, 0)

To Reproduce
Compare two of the following tests

  1. This works

    // Arrange
    
    var service = Substitute.For<IProductService>();
    
    service
        .When(e => e.ReduceProductAvailability(Arg.Any<int>(), Arg.Any<int>()))
        .Do(x => throw new NotFoundException("XYZ"));
    
    // Act
    
    Action result = () => service.ReduceProductAvailability(0, 0);
    
    // Assert
    
    result.Should().Throw<NotFoundException>();
  2. This not

    // Arrange
    
    var service = Substitute.For<IProductService>();
    
    service
        .When(e => e.ReduceProductAvailability(Arg.Any<int>(), Arg.Any<int>()))
        .Do(x => throw new NotFoundException("XYZ"));
    
    // Act
    
    Action result = () => service.ReduceProductAvailability(Arg.Any<int>(), Arg.Any<int>());
    
    // Assert
    
    result.Should().Throw<NotFoundException>();

Expected behaviour
It should throws the exception in both cases

Environment:

  • NSubstitute version: 3.1.0
  • XUnit: 2.4.1
  • FluentAssertions: 5.5.0
  • Platform: dotnetcore2.1 project on Windows
@tpodolak
Copy link
Member

tpodolak commented Nov 15, 2018

The second examples do not work because you are using argument matcher in a real call.
See documentation . In general Arg matchers should only be used for mock setup and checking received calls. See also feature request nsubstitute/NSubstitute.Analyzers#35 (comment) for NSubstitute.Analyzers with quick description about the proper usage

@mkolumb
Copy link
Author

mkolumb commented Nov 15, 2018

Hi,

You're right.
I checked the documentation, but I didn't find this.
Thank you, I will close this issue.

@mkolumb mkolumb closed this as completed Nov 15, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants