By Lucas Valle


2019-07-11 19:01:28 8 Comments

I make the next moq call:

var mock = new Mock<IPagoService>();
mock.Setup(m => m.GetCodigoAutorizacion(Guid.NewGuid())).Returns("e");
string p = mock.Object.GetCodigoAutorizacion(Guid.NewGuid());

Why is the variable p null?

4 comments

@Julian 2019-07-11 19:05:05

When you have:

mock.Setup(m => m.GetCodigoAutorizacion("A")).Returns("B");

You will tell, if invoked with "A", then return "B". Note, If invoked with something else, e.g, "C", it returns the default value of the type.

So you need, if invoked with any then GUID returns "e" - you could use It.IsAny<T>().

So for this case:

mock.Setup(m => m.GetCodigoAutorizacion(It.IsAny<Guid>())).Returns("e");

@Ernestas Romeika 2019-07-11 19:08:46

It's because the two Guid.NewGuid() calls generate two different guid values, and so when you're calling the mockup you're passing and entirely new guid from the one that was set up. You can either save the initial guid value and reuse it, or if you don't mind what guid it is you can use:
It.IsAny<Guid>() in the setup to accept any guid.

@juharr 2019-07-11 19:05:19

You're setup is using one Guid and the call another. Since the setup and the call do not match it returns the default. Instead assign the Guid to a variable and use that.

var mock = new Mock<IPagoService>();
var id = Guid.NewGuid();
mock.Setup(m => m.GetCodigoAutorizacion(id)).Returns("e");
string p = mock.Object.GetCodigoAutorizacion(id);

If you need to handle any Guid then do this instead

mock.Setup(m => m.GetCodigoAutorizacion(It.IsAny<Guid>())).Returns("e");

@Vidmantas Blazevicius 2019-07-11 19:04:27

Guid.NewGuid() value will be different each time so it doesn’t satisfy your setup condition.

You can use Guid.Empty or some constant value to make this work.

Related Questions

Sponsored Content

26 Answered Questions

[SOLVED] Why not inherit from List<T>?

3 Answered Questions

[SOLVED] Using Moq to mock an asynchronous method for a unit test

45 Answered Questions

[SOLVED] Deep cloning objects

  • 2008-09-17 00:06:27
  • NakedBrunch
  • 766641 View
  • 2105 Score
  • 45 Answer
  • Tags:   c# .net clone

296 Answered Questions

[SOLVED] Hidden Features of C#?

  • 2008-08-12 16:32:24
  • Serhat Ozgel
  • 676817 View
  • 1475 Score
  • 296 Answer
  • Tags:   c# hidden-features

2 Answered Questions

[SOLVED] moq objects Returns method, should return a null object

5 Answered Questions

[SOLVED] How to verify that method was NOT called in Moq?

  • 2009-02-11 15:24:21
  • alex
  • 65519 View
  • 435 Score
  • 5 Answer
  • Tags:   c# .net moq

5 Answered Questions

[SOLVED] How to return null when accessing a moq object?

  • 2012-07-16 10:32:53
  • fhnaseer
  • 8137 View
  • 2 Score
  • 5 Answer
  • Tags:   c# unit-testing moq

3 Answered Questions

[SOLVED] Moq mock method with out specifying input parameter

  • 2011-10-19 19:34:15
  • AnonyMouse
  • 53300 View
  • 123 Score
  • 3 Answer
  • Tags:   c# mocking moq

1 Answered Questions

[SOLVED] Why is my Moq not returning null?

  • 2016-03-17 17:57:50
  • user3099140
  • 65 View
  • 1 Score
  • 1 Answer
  • Tags:   c# moq

1 Answered Questions

[SOLVED] Moq object always returns null - why?

  • 2011-07-04 03:05:04
  • Gumby
  • 2466 View
  • 0 Score
  • 1 Answer
  • Tags:   unit-testing moq

Sponsored Content