How can I check if an anonymous function has been called with NSubstitute?

Marius

I want to check if an anonymous function has been called with NSubstitute. A method in a class I have takes a Func<> parameter, and I want to make sure this parameter is called (or not called). I have tried the following, but it does not seem to work:

var spy = Substitute.For<Func<string, int>>();
MyClass.DoSomething(spy);
spy.Invoke(Arg.Any<string>()).Received();

This however throws an exception:

NSubstitute.Exceptions.NullSubstituteReferenceException : NSubstitute extension methods like .Received can only be called on objects created using Substitute.For<T>() and related methods.
khlr

Try replacing

spy.Invoke(Arg.Any<string>()).Received();

with

spy.Received().Invoke(Arg.Any<string>());

There's no difference if your production code calls the func via spy.Invoke() or via spy(). But the test won't succeed, if your code calls BeginInvoke. But I think that shouldn't be a problem, because you should know in advance if a test for Invoke or BeginInvoke is needed.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Nsubstitute check if setter has been called

From Java

How can I test that a function has not been called?

From Dev

Scheme: How to check if a function has been called with a same argument

From Dev

How can I repeat functions in F# after a function has been called inside it?

From Dev

Check if the function has been called in gtest

From Dev

How can I verify that RemoveRange has been called on a mock DbContext?

From Dev

How to check if a certain method has been called?

From Dev

How can I check if an appearance property has been set?

From Dev

How can I check if file has been downloaded in ansible

From Dev

How can i check to see if a row has been selected?

From Dev

How can I check if jcombobox selection has not been selected?

From Dev

How can I check if a bean has been loaded by springboot

From Dev

How can i check the radiobutton that it has been checked in this table?

From Dev

How to determine if a function has been called in Powershell

From Dev

How to check how many times a recursive function has been called in Java?

From Dev

How to check how many times a recursive function has been called in Java?

From Dev

Elixir check if a function has been called from a ExUnit test?

From Dev

Check the conditions every time after function has been called

From Dev

sinon spy check if function has been called error

From Dev

How can I check if my function is called with input from a pipe?

From Dev

Perl: How to check if CGI::header has already been called?

From Dev

How can I add a mapping in AutoMapper after Initialize has been called?

From Dev

How can I tell if net/http's ResponseWriter.Write() has been called?

From Dev

How can I know that onCreateView has been called from an outer class?

From Dev

How can I verify that a method has been called with intern test framework?

From Dev

a socket fd has been called "shutdown", can I "reopen" it?

From Dev

How to test how many times a function has been called

From Dev

How to test how many times a function has been called

From Dev

How to test that a function has been called after an event was fired?

Related Related

  1. 1

    Nsubstitute check if setter has been called

  2. 2

    How can I test that a function has not been called?

  3. 3

    Scheme: How to check if a function has been called with a same argument

  4. 4

    How can I repeat functions in F# after a function has been called inside it?

  5. 5

    Check if the function has been called in gtest

  6. 6

    How can I verify that RemoveRange has been called on a mock DbContext?

  7. 7

    How to check if a certain method has been called?

  8. 8

    How can I check if an appearance property has been set?

  9. 9

    How can I check if file has been downloaded in ansible

  10. 10

    How can i check to see if a row has been selected?

  11. 11

    How can I check if jcombobox selection has not been selected?

  12. 12

    How can I check if a bean has been loaded by springboot

  13. 13

    How can i check the radiobutton that it has been checked in this table?

  14. 14

    How to determine if a function has been called in Powershell

  15. 15

    How to check how many times a recursive function has been called in Java?

  16. 16

    How to check how many times a recursive function has been called in Java?

  17. 17

    Elixir check if a function has been called from a ExUnit test?

  18. 18

    Check the conditions every time after function has been called

  19. 19

    sinon spy check if function has been called error

  20. 20

    How can I check if my function is called with input from a pipe?

  21. 21

    Perl: How to check if CGI::header has already been called?

  22. 22

    How can I add a mapping in AutoMapper after Initialize has been called?

  23. 23

    How can I tell if net/http's ResponseWriter.Write() has been called?

  24. 24

    How can I know that onCreateView has been called from an outer class?

  25. 25

    How can I verify that a method has been called with intern test framework?

  26. 26

    a socket fd has been called "shutdown", can I "reopen" it?

  27. 27

    How to test how many times a function has been called

  28. 28

    How to test how many times a function has been called

  29. 29

    How to test that a function has been called after an event was fired?

HotTag

Archive