Create expected result for the controller that will return BadRequestResult upon when exception occures in controller

here is the code for my test


        [Fact]
        public void ActivityController_GetStores_ReturnBadRequest()
        {

            var stores = new QueryStringParams();

            var result = controllerWithNullServices.GetStores(stores);
            var expected = result;
            result.Should().BeOfType<BadRequestObjectResult>().Which.Should().BeEquivalentTo(expected);
        }

GetStores(stores) function returns a BadRequestObjectResult which I need to expect in my code of test. As for now I’ve written expected = result which will setup the expected value to exactly match the result.

here is the code for my controller as well

 public IActionResult GetStores([FromBody] Helper.QueryStringParams queryParam)
 {
     _customLoggerService.LogInformation($"Getting Activity Log with params :: {queryParam} ");
     try
     {
         Helper.PagedResult<List<ActivityLogDto>> result = _activityLogService.GetActivityLog(queryParam);
         return Ok(result);
     }
     catch (Exception ex)
     {
         string logMessage = $"Error getting Activity Log." +
                             $" n Message :: {ex.Message} n Exception :: {ex}";
         _customLoggerService.LogError(logMessage);

         return BadRequest(new
         {
             Errors = ex,
             ex.Message
         });
     }

 }

so here bad requests is being returned only when some sort of exception occurs in the code and further more BadRequest() is also over loaded as below

public override BadRequestObjectResult BadRequest(object error)
{

    if (error is not string)
    {
        Dictionary<string, object> errorDictionary = error.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public)
            .ToDictionary(prop => prop.Name, prop => prop.GetValue(error, null));

        return base.BadRequest(new SharedResponse.Response()
        {
            Status = SharedResponse.Response.RequestStatus.Error,
            Message = errorDictionary["Message"].ToString(),
            Errors = errorDictionary["Errors"]
        });
    }
    return base.BadRequest(new SharedResponse.Response()
    {
        Status = SharedResponse.Response.RequestStatus.Error,
        Message = error.ToString(),
        Errors = null
    });
}

how can I manually create expected value for the test case as described above

I want to write the expected value for this code manually not just the type but complete expected value.

[Fact]
        public void ActivityController_GetStores_ReturnBadRequest()
        {

            var stores = new QueryStringParams();

            var result = controllerWithNullServices.GetStores(stores);
            var expected = result;
            result.Should().BeOfType<BadRequestObjectResult>().Which.Should().BeEquivalentTo(expected);

New contributor

Ahad is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật