Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to mock mediator in controller test #72

Closed
adnancindioglu opened this issue Jun 14, 2022 · 0 comments
Closed

Unable to mock mediator in controller test #72

adnancindioglu opened this issue Jun 14, 2022 · 0 comments

Comments

@adnancindioglu
Copy link

[Authorize]
[Route("[controller]")]
[ApiController]
public class BaseApiController : Controller
{
 private IMediator _mediator;
 protected IMediator Mediator => _mediator ??= HttpContext.RequestServices.GetService<IMediator>();
}

I don't have constructor to mock mediator so i can't set my result value.
Transactions in "GetBankImageQuery" return null or false.

[ApiController]
public class BankImagesController : BaseApiController
{
        [HttpGet("getByBankId")]
        public async Task<IActionResult> GetByBankId(long bankId)
        {
            var result = await Mediator.Send(new GetBankImageQuery { BankId = bankId });
           
            if (!result.Success)
            {
                return BadRequest(result.Message);
            }
            return File(result.Data.Image, result.Data.ContentType);
      
        }
}

Unit test example in architecture:


[Test]
public async Task BankImages_GetByBankId_Success()
{
    const string requestUri = "api/BankImages/getByBankId?bankId=1";
    string token = MockJwtTokens.GenerateJwtToken(ClaimsData.GetClaims());

    var requestMessage = new HttpRequestMessage(HttpMethod.Get, requestUri);
    requestMessage.Headers.Authorization = new AuthenticationHeaderValue("Bearer", token);

    var response = await HttpClient.SendAsync(requestMessage);

    Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
}

How should I proceed here?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants