lundi 29 août 2016

Unit Testing for Web API, GET same ID

I'm at a lost for how to write a unit test for my web api GET by id method.

Here is what I have:

    public void GetProduct_ShouldReturnSameID()
    {
        var context = new TestModelContext();
        context.Products.Add(GetDemoProduct());

        var controller = new ProductsController(context);
        var result = controller.GetProduct(3) as OkNegotiatedContentResult<Product>;

        Assert.IsNotNull(result);
        Assert.AreEqual(3, result.Content.Id);
    }

And my controller method I'm trying to test

    public IHttpActionResult GetProduct(int id)
    {
        var product = (from t in db.Products.Include(t => t.Reviews)
                              .Where(t => t.Id == id)
                              select t);

        if (product == null || product.Count() == 0)
        {
            return NotFound();
        }

        return Ok(product);
    }

My test works find with my other controllers, but just not this one. I was wondering what is wrong with this? My test fails with an

"Expected: not null But was: null"

Aucun commentaire:

Enregistrer un commentaire