lundi 1 août 2016

IQueryable Unit Test

I have a web api and I am exposing a endpoint like so:

api/Holiday?name={name}

This is the controller get method for the web api:

    public IQueryable<Holiday> GetHolidayByName(string name)
    {
        return db.Holiday.Where(n => string.Equals(n.Name, name));
    }

How can I write a unit test for this that checks the names are equal? I can check the result is not null however bit confused how I can check the names are equal:

    [TestMethod]
    public void GetHoliday_GetHolidayByName()
    {
        // Arrange
        HolidaysController controller = new HolidaysController();

        // Act
        IQueryable<Holiday> actionResult = controller.GetHolidayByName("Spain");

        //Assert
        Assert.IsNotNull(actionResult);

        //any attempt to check names are equal results in a fail
        //For instance this fails
        var result = controller.GetHolidayByName("Spain") as OkNegotiatedContentResult<Holiday>;
        Assert.AreEqual("Spain", result.Content.Name);


    }

Aucun commentaire:

Enregistrer un commentaire