Search code examples
c#unit-testingasp.net-web-apiodataasp.net-web-api-odata

How to test the patch odata webapi method with Delta<Tentity> which has some collection properties


I need to test the following Patch method in my test project.

    public async Task<IHttpActionResult> PatchMarkAsReadAlertResults([FromODataUri] Guid key, Delta<MarkAsReadAlertResult> result)
    {
        await AlertResultsHelper.UpdateAlertResultStatus(key, result.GetEntity(), alertResultsActionsServiceProvider, KeyEvent);
        return Updated(result);
    }

I have written my test cases like this

    [TestMethod]
    public async Task AlertProfileMarkAsReadAlertResultsTest()
    {
        #region Arrange
        Guid key = Guid.Parse("e6f940d5-2ffb-4ff3-b7c1-04aa2514a37e");

        var alerts = new MarkAsReadAlertResult();

        var results = new Delta<MarkAsReadAlertResult>();

        alerts.ResultIds = new List<string>();


        alerts.ResultIds.Add("906433381");

        results.TrySetPropertyValue("ResultIds", alerts);

        IHttpActionResult result = null;
        #endregion

        #region Act
        result = await this.alertProfilesController.PatchMarkAsReadAlertResults(key, results);
        #endregion

        #region  Assert
        Assert.IsNotNull(result);
        #endregion
    }

Am getting Object reference error here

results.TrySetPropertyValue("ResultIds", alerts);

Not sure why am getting it up here am i missing something?

I have verified this Testing the Patch odata webapi method but that is not for list of string.


Solution

  • I found out the mistake i made. Instead of setting the property alone i send the whole object.

    Old Code

    results.TrySetPropertyValue("ResultIds", alerts);
    

    Updated Code

    results.TrySetPropertyValue("ResultIds", alerts.ResultIds);