0

I am completely new to writing TestMethod. I am trying to write a MSTest Test Method as below to check if the Service Layer returns valid GUID

[TestMethod]
        public async Task RegisterAsync_Works()
        {
            //Arrange
            var ranInfo = new RanInfo
            {
                VendorId = 10,
                RanDetail = new List<RanDetail>()
                {
                    new RanDetail{FieldId = 1,Value = "demo@abc.com"},
                    new RanDetail{FieldId = 2,Value = "aaa"},
                }
            };
            var registrationKey = Guid.Parse("42b184f0-7c8f-4755-80fc-1907cdb2c736");

            this.mockRegistrationService.Setup(r => r.RegisterAsync("user", ranInfo))
                .ReturnsAsync(registrationKey).Verifiable();  //<=== Statement #EXPECTED

            //Act
            var result = await this.registrationController.RegisterAsync(ranInfo);

            //Assert
            var okObject = result as OkObjectResult;
            Assert.AreEqual(200, okObject.StatusCode);
        }

And my Controller is

 public async Task<IActionResult> RegisterAsync(
               [FromBody, Required] RanInfo ranInfo)
        {
            try
            {
                if (ranInfo == null) 
                    return this.BadRequest($"{nameof(ranInfo)} payload is required");


                var result = await this.ranService.RegisterAsync(  //<=== Statement #A (  result ALWAYS HAS EmptyGUID)
                                   this.User,
                                   ranInfo);
                if (result==null )
                return this.BadRequest("The registration failed due to invalid credentials");
                    return Ok(result);
            }
            
            catch (Exception ex)
            {
                return this.BadRequest(ex.Message);
            }
        }

Statement #A marked above ==> result ALWAYS HAS EmptyGUID though I set in Test Method (marked as <=== Statement #EXPECTED in above code) with valid Guid.

Why is the Statement #A never get assigned with the set Guid value.

Please help me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.