0

I am creating web api for a songsDB and I am trying to test API GET methods which return a Json string of songs in a list. The get method in question uses a linq queries and returns the songs as expected but am unable to write a unit test for this method and many like it with a concern for code coverage also. The app is hosted on azure and the data is being retrieved from an sql db instance in azure. The method displays the 5 cheapest albums in the db in ascending order. Any help is greatly appreciated.

//API GET Method
[HttpGet("leastExpensive")]
[ProducesResponseType(404)]
[ProducesResponseType(200)]
public ActionResult<Tunes> GetCheapestAlbums()
{
    var price = _context.Songs
        .OrderBy(p => p.Price)
        .Select(g => new
    {
        g.ChartPosition,
        g.Artist,
        g.Genre,
        g.Price
    }).Take(5)
    .ToList();
    if (price == null)
    {
        return NotFound();
    }
    else
    {
        return Ok(price);
    }
}

//Json response

[{
    "chartPosition":29,"artist":"ABBA","genre":"Pop","price":4.39
},{
    "chartPosition":60,"artist":"Amy Whinehouse","genre":"Pop","price":5.05
},{
    "chartPosition":10,"artist":"BB King","genre":"Blues","price":5.5
},{
    "chartPosition":15,"artist":"Kenny Rogers","genre":"Country","price":5.92
},{
    "chartPosition":17,"artist":"Pearl Jam","genre":"Rock","price":6.29
}]

I am looking to assert the Json string as equal

Igor
  • 55,253
  • 10
  • 80
  • 149
  • Where does `type` come from, i'd assume you meant `return Ok(price)` – JSteward Mar 28 '19 at 19:23
  • Unless I'm missing something the best you can do here is mock `_context` and setup it's response. That will allow you to verify the linq query is correct. – JSteward Mar 28 '19 at 19:25
  • @JSteward apologies yes "type" should be "price". Do have any more information on how i could mock _context? – Ceiran McCabe Mar 29 '19 at 12:36

0 Answers0