jeudi 30 avril 2015

Unit Testing Web Api Controller that uses IHttpActionResult and ado.net

I'm trying to carry out some unit tests on my API Controller however I'm having issues since it is using IHttpActionResult and my actual data is called from a database (Azure).

Here's a sample of a simple Get by id method

// GET: api/RoundOnesApi/5
    [ResponseType(typeof(RoundOne))]
    public IHttpActionResult GetRoundOne(int id)
    {
        RoundOne roundOne = db.RoundOnes.Find(id);
        if (roundOne == null)
        {
            return NotFound();
        }

        return Ok(roundOne);
    }

I then tried to make a unit test to test this.

 public void TestMethod1()
    {
        //ApplicationDbContext db = new ApplicationDbContext();


        //arrange

        var controller = new RoundOnesApiController();

        //act

        var actionResult = controller.GetRoundOne(1); //1

        //assert

        var response = actionResult as OkNegotiatedContentResult<RoundOne>; //RoundOne is my model class

        Assert.IsNotNull(response);

        Assert.AreEqual(1, response.Content.Id);

GetRoundOne(1) contains a database entry of a football teams information. Since this is not null I assumed it would pass.

By the way I'm just looking to do a general Unit Test to see if GetRoundOne(1) can be tested against actually existing. Once it passes that's all I need.

Aucun commentaire:

Enregistrer un commentaire