chevron-thin-right chevron-thin-left brand cancel-circle search youtube-icon google-plus-icon linkedin-icon facebook-icon twitter-icon toolbox download check linkedin phone twitter-old google-plus facebook profile-male chat calendar profile-male
0 votes
I am trying to write an integration test that will call the rate controller and save a new rate. I seem to be unable to correctly mock the OKNegotiatedContentResult or at least I am unable to step into the logicRepo.SaveRate method.

What am I doing wrong here?

Code under test:

public IHttpActionResult SaveRateWithReturnValue(Rate Rate)

        {

            var checkResult = _utilityRepo.CheckWebServicePrerequisites(ActionEnums.eActions.RatesUse, ActionEnums.eActions.RatesEdit);

            if (checkResult.OperationStatus.HasValue && checkResult.OperationStatus.Value)

            {

                return Ok(_logicRepo.SaveRate(Rate));

            }

            return Ok(checkResult);

        }

My Test:

public void A_New_Rate_Can_Be_Successfully_Saved()

        {

            Settings temp = new Settings

            {

                Database = \"MyDB\",

                LoggingLevel = \"4\",

                Password = \"\",

                SQLDataSource = \"\",

                UserId = \"\"

            };

            SKIDATA.SDUSA.PlateTechAdmin.Rate pr = new SKIDATA.SDUSA.PlateTechAdmin.Rate()

            {

                Comments = \"Initial Comment\",

                RateID = \"ABC123\",

                UpdateStaffRecNum = 5,

                Used = true

            };

            PTMessage retMsg = new PTMessage

            {

                OperationStatus = true

            };

            var utilRepo = Isolate.Fake.NextInstance<PTUtilityRepository>(Members.ReturnRecursiveFakes);

            var ok = Isolate.Fake.NextInstance<System.Web.Http.Results.OkNegotiatedContentResult<object>>(Members.CallOriginal);

            Isolate.WhenCalled(() => utilRepo.CheckWebServicePrerequisites(ActionEnums.eActions.AuthorizationsUse, ActionEnums.eActions.AuthorizationsEdit)).WillReturn(retMsg);

            Isolate.Fake.NextInstance<PlateRateRepositoryLogic>(Members.ReturnRecursiveFakes);

            RateController prc = new RateController();

            prc.SaveRateWithReturnValue(pr);

        }
asked by Mark (4.1k points)

1 Answer

0 votes

Hi Mark,

We didn't understand completely what did you want to check.

After discussion with our Developer, this test looks suitable for your case:

We used  Isolate.NonPublic.InstanceField(prc, "_logicRepo").Value = plateRateRepositoryLogic;

instead of FakeNextInstance.



var pr = new Rate()
{
Comments = "Initial Comment",
RateID = "ABC123",
UpdateStaffRecNum = 5,
Used = true
};
var prc = new RateController();
var ptutilityRepository = Isolate.Fake.Instance<PTUtilityRepository>();
Isolate.NonPublic.InstanceField(prc, "_utilityRepo").Value = ptutilityRepository;
var message = new PTMessage { OperationStatus = true };
Isolate.WhenCalled(() =>
ptutilityRepository.CheckWebServicePrerequisites(null,null)).WillReturn(message);
var plateRateRepositoryLogic = Isolate.Fake.Instance<PlateRateRepositoryLogic>();
Isolate.NonPublic.InstanceField(prc, "_logicRepo").Value = plateRateRepositoryLogic;
Isolate.WhenCalled(() => plateRateRepositoryLogic.SaveRate(null)).WillReturn(message);
var result = prc.SaveRateWithReturnValue(pr);
// assert
// ObserverId=0
Assert.IsInstanceOf<System.Web.Http.Results.OkNegotiatedContentResult<WebApplication4.Controllers.PTMessage>>(result);

Please keep us posted on the output.

Cheers,

Alon Sapozhnikov

Support Specialist.

answered by Alon_TypeMock (8.9k points)

Hi Guys,

Thanks for the response, it did come in handy but not quite what I was looking for. I did figure it out though. Let me try to clarify.

The RateController.SavePlateRateWithReturnValue method returns the OkNegotiatedContentResult with a content created by this call chain:

LogicRepository.SaveRate (Rate rate) ->

    Rate.SaveRate(int staff, bool open) ->

        PRate.SavePRate ( string key, bool open) ->

            DBRate.SavePR(Rate, rate) ->

                DB.ExecuteScalar(string procName, Rate rate) (OLEDB layer)

I want to fake the DB.ExecuteScalar method or the DBRate.SavePR method in so that a database is not required in order to run the test.

My basic problem was fuzzy thinking I did not have to fake the OkNegotiatedContentResult, just the DBRate method and let TypeMock take care of doing that at the right time.

My Current Test successful code looks like this:

[TestMethod]

        public void A_New_PlateRate_Can_Be_Successfully_Saved()

        {

            Settings temp = new Settings

            {

                Database = "PlateTech",

                LoggingLevel = "4",

                Password = "",

                SQLDataSource = "",

                UserId = ""

            };

 

            SKIDATA.SDUSA.PlateTechAdmin.PlateRate pr = new SKIDATA.SDUSA.PlateTechAdmin.PlateRate()

            {

                Comments = "Initial Comment",

                PlateNumber = "ABC123",

                UpdateStaffRecNum = 5,

                Used = true

            };

 

            PTMessage retMsg = new PTMessage

            {

                OperationStatus = true

            };

 

 

            var utilRepo = Isolate.Fake.NextInstance<PTUtilityRepository>(Members.ReturnRecursiveFakes);

            Isolate.WhenCalled(() => utilRepo.CheckWebServicePrerequisites(ActionEnums.eActions.AuthorizationsUse, ActionEnums.eActions.AuthorizationsEdit)).WillReturn(retMsg);

            var fakePlateRate = Isolate.Fake.AllInstances<DBPlateRate>(Members.ReturnRecursiveFakes);

 

            PlateRateController prc = new PlateRateController();

            OkNegotiatedContentResult<PTMessage> result = prc.SavePlateRateWithReturnValue(pr) as OkNegotiatedContentResult<PTMessage>;

 

            Isolate.Verify.WasCalledWithExactArguments(() => fakePlateRate.SavePlateRate(pr));

 

            Assert.AreEqual(true, result.Content.OperationStatus);

            Assert.AreEqual("Plate Rate saved.", result.Content.MessageText);

            Assert.AreEqual(0, result.Content.ReturnObject.Count);

        }

    }

Thanks very much for your help.

Hi Mark,

Thank you for that detailed reply.

I'm glad to hear you were able to achieve your goals.

If you have any other questions, don't hesitate to contact us.

Cheers,

Alon Sapozhnikov

Support Specialist

...