1

I'm trying to write test cases for my web api methods.

 [Fact]
 public async Task Get_Message_By_Id()
 {
    var messages = new Messages()
    {
        MessageId = 1,
        Recipient = "1;2;3",
        Subject = "Test Subject",
        MessageText = "test subject"
    };

    var mesBL = new Mock<IMessagesBusinessLogic>();
        mesBL
            .Setup(repo => repo.GetRecivedMessageById(It.IsAny<IWebToken>() ,messages.MessageId))
            .Returns(Task.FromResult(messages));

    var messageController = new MessagesController(mesBL.Object);

    var contentResult = await messageController.GetRecivedMessageById(messages.MessageId) as OkObjectResult; 

    Assert.NotNull(contentResult);
 }

I get an error of null reference while GetRecivedMessageById method call.

Controller method;

[HttpGet]
[Route("{id:int}")]
public async Task<IActionResult> GetRecivedMessageById(int id)
{
    return Ok(await _messagesBusinessLogic.GetRecivedMessageById(User.GetWebToken(), id));
}

Here, issue is because, user identity passing NULL.

How can we pass it from Test?

Hina Khuman
  • 757
  • 3
  • 14
  • 41
  • 1
    Possible duplicate of [Mocking IPrincipal in ASP.NET Core](http://stackoverflow.com/questions/38557942/mocking-iprincipal-in-asp-net-core) – Nkosi Dec 30 '16 at 13:11

1 Answers1

1

User is null because it was not setup in the test before exercising the method under test.

//Create test user
var displayName = "User name";
var role = "SomeRole";

var token = new Mock<IWebToken>();
token.Setup(m => m.DisplayName).Returns(displayName);
token.Setup(m => m.Role).Returns(role);

var identity = new UserIdentity(token.Object);

var principal = new GenericPrincipal(identity, roles: new string[] { });
var user = new ClaimsPrincipal(principal);
// Set the User on the controller via the ControllerContext
var messageController = new MessagesController(mesBL.Object) {
    ControllerContext = new ControllerContext()
    {
        HttpContext = new DefaultHttpContext() { User = user }
    }
};
Nkosi
  • 235,767
  • 35
  • 427
  • 472
  • Ok this is for web api 2 as tagged in post You may need to remove that tag. Core's User is read only. checking on how to set that now. most probably it's through HttpContext. – Nkosi Dec 30 '16 at 13:05
  • Okay, updated answer not gives any error, let me check it. – Hina Khuman Dec 30 '16 at 13:11
  • just noticed that you are casting to `UserIdentity` in extension method. you would need to modify test setup to match that expectation. Simple change of the `GenericIdentity` in example. – Nkosi Dec 30 '16 at 13:12
  • @HinaKhuman where is this `UserIdentity` class defined. cannot seem to find it through searched online. – Nkosi Dec 30 '16 at 13:24
  • Ok I see. Right. then all you have to do is create that identity and assign it to a principal like in the example and you should be good to go. – Nkosi Dec 30 '16 at 13:36
  • May I have one more favor please, Is there any good doc. to start with Mock ? – Hina Khuman Dec 30 '16 at 13:40