0

I have problem with mocking some method:

Interface method:

bool IsUserAuthorizedToAction(AuthorizationContextData contextData, AuthorizedActionType actionType);

How is called:

_userInformation.IsUserAuthorizedToAction(new AuthorizationContextData(brandId),
            AuthorizedActionType.DeleteCampaign);

And I want to return true or false for tests... But it always return false!

How I mock it:

userInformation.IsUserAuthorizedToAction(
            Arg.Is<AuthorizationContextData>(acd => acd == new AuthorizationContextData("validId")),
             Arg.Any<AuthorizedActionType>()).Returns(true);

Returns false - not working.

            userInformation.IsUserAuthorizedToAction(Arg.Any<AuthorizationContextData>(), Arg.Any<AuthorizedActionType>()).Returns(true);

Still not working

Do you have any idea why and how to make it work?

Nerf
  • 938
  • 1
  • 13
  • 30
  • 1
    Does `AuthorizationContextData` override `Equals`? – Mark Seemann Oct 27 '16 at 16:59
  • No. public class AuthorizationContextData { public string BrandId { get; set; } public AuthorizationContextData(string brandId) { BrandId = brandId; } } – Nerf Oct 28 '16 at 06:46
  • You're creating a _new_ instance of `AuthorizationContextData`, which by default has reference equality, and compare it against _another_ instance. Can these two objects ever be the _same_ object? – Mark Seemann Oct 28 '16 at 07:17
  • Arg.Any is the resolution – Nerf Oct 28 '16 at 09:04

0 Answers0