NSubstitute - setting a property of the calling object

Rich Bryant

I have a in issue where I would like to swap out Moq for NSubstitute entirely. In most cases, this is extremely straightforward, however I have run across a fairly specialized issue.

Here's the Moq code.

_registrationCommandHandler.Setup(c => c.Execute
(It.Is<CheckUniqueUserCommand>(r => r.Request.UserName == "fred"))).
                Callback((CheckUniqueUserCommand c) =>
                {
                    c.Response = new CheckUserNameIsUniqueResponse()
                    {
                        IsUnique = true,
                        Success = true
                    };
                    c.Success = true;
                });

The closest I seem to be able to get with NSubstitute is

 _registrationCommandHandler.When(c => c.Execute    
(Arg.Any<CheckUniqueUserCommand>())).Do
            ((CheckUniqueUserCommand c) =>
            {
              c.Response = new __Internal.CheckUserNameIsUniqueResponse()
              {
                  IsUnique = true,
                  Success = true
              };
            c.Success = true;
          });

which won't even compile. This leaves me a bit stuck. Does anyone have any suggestions?

David Tchepak

I'm guessing a bit here, but try:

 _registrationCommandHandler
    .When(c => c.Execute(Arg.Is<CheckUniqueUserCommand>(r => r.Request.UserName == "fred")))
    .Do(call => {
          var c = call.Arg<CheckUniqueUserCommand>();
          c.Response = new __Internal.CheckUserNameIsUniqueResponse()
          {
              IsUnique = true,
              Success = true
          };
          c.Success = true;
    });

NSubstitute does not do the same argument passing as Moq does for callbacks. Instead it passes a parameter with information about the call, and you can access the arguments using call.Arg<T> or call[i].

In addition to changing the .Do block, I also switched from using Arg.Any(..) to Arg.Is(..) to closer match the Moq sample.

Hope this helps.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Randomly calling an object property

From Dev

Calling the property of an object returns object instead of property

From Dev

Setting object property with ternary operator

From Dev

React object property setting broken

From Dev

Setting enumerable property on object prototype

From Dev

Setting a property value of javascript object

From Dev

JS Calling Object Property by Variable

From Dev

Is setting object property:!object.propety volatile?

From Dev

Setting object property to Array Literal throwing error

From Dev

Setting a custom object to a Spinner and showing a specific property

From Dev

Setting and getting a boolean object property in JavaScript

From Dev

Is it possible to delete a property from an object by setting it to undefined?

From Dev

Setting nested object property by string path

From Dev

Javascript not setting property of undefined in prototyped object

From Dev

Setting a get-only property on an object in a collection

From Dev

Knockout - change an object's 'subclass' by setting a property on it

From Dev

Setting the type of variable from an object property

From Dev

Getting error setting a property on the result object in ibatis

From Dev

Struct not calling property observers on new object

From Dev

Anonymous Problem while calling object property

From Dev

Laravel Blade: calling property in object results in NULL

From Dev

Calling the value of a variable as a property of an object in JavaScript

From Dev

Javascript - calling an external function as a property of an object

From Dev

calling function stored as a property in an object in a for loop

From Dev

Setting an object's property overwrites this property in objects of same types

From Dev

Grails new Domain object with property map not setting property

From Dev

Calling a function within an object property from another property

From Dev

Ramda Js: Setting property on an object using a value from the same object

From Dev

Calling method of object property in mocked object results in NullPointerException

Related Related

  1. 1

    Randomly calling an object property

  2. 2

    Calling the property of an object returns object instead of property

  3. 3

    Setting object property with ternary operator

  4. 4

    React object property setting broken

  5. 5

    Setting enumerable property on object prototype

  6. 6

    Setting a property value of javascript object

  7. 7

    JS Calling Object Property by Variable

  8. 8

    Is setting object property:!object.propety volatile?

  9. 9

    Setting object property to Array Literal throwing error

  10. 10

    Setting a custom object to a Spinner and showing a specific property

  11. 11

    Setting and getting a boolean object property in JavaScript

  12. 12

    Is it possible to delete a property from an object by setting it to undefined?

  13. 13

    Setting nested object property by string path

  14. 14

    Javascript not setting property of undefined in prototyped object

  15. 15

    Setting a get-only property on an object in a collection

  16. 16

    Knockout - change an object's 'subclass' by setting a property on it

  17. 17

    Setting the type of variable from an object property

  18. 18

    Getting error setting a property on the result object in ibatis

  19. 19

    Struct not calling property observers on new object

  20. 20

    Anonymous Problem while calling object property

  21. 21

    Laravel Blade: calling property in object results in NULL

  22. 22

    Calling the value of a variable as a property of an object in JavaScript

  23. 23

    Javascript - calling an external function as a property of an object

  24. 24

    calling function stored as a property in an object in a for loop

  25. 25

    Setting an object's property overwrites this property in objects of same types

  26. 26

    Grails new Domain object with property map not setting property

  27. 27

    Calling a function within an object property from another property

  28. 28

    Ramda Js: Setting property on an object using a value from the same object

  29. 29

    Calling method of object property in mocked object results in NullPointerException

HotTag

Archive